Python: Bitwise NOT and List

https://news.ycombinator.com/item?id=147962
by kirubakaran • 18 years ago
24 22 18 years ago

I realized that instead of using negative numbers to access elements of a list from the end, we can just use bitwise NOT. That looks more symmetrical (and hence beautiful).

For a list x:

First element is x[0]

Last element is x[~0] (instead of x[-1])

You can use x[~0], x[~1], x[~2]... (coz ~0==-1, ~1==-2 etc)

Did you guys know this already or is this my fresh insight? If it is the latter, YAY! :-)

Related Stories

Loading related stories...

Source preview

news.ycombinator.com