Today I learned about the symmetry in indexing from the beginning and end of a list with the bitwise invert operator.
As you might be aware of, Python allows indexing with negative indices:
>>> s = "Hello, there!"
>>> s[-1]
'!'
In case this is new to you, you can check out this Pydon't on the subject.
One thing to note is that the index for the n
th element is n - 1
,
but the index for the n
th element from the end is -n
.
This is just “how it works”, but it is kind of a bummer because it is very asymmetrical:
>>> seq = "ABCDCBA"
>>> seq[0], seq[-1] # 0 and -1
('A', 'A')
>>> seq[1], seq[-2] # 1 and -2
('B', 'B')
>>> seq[2], seq[-3] # 2 and -3
('C', 'C')
>>> seq[3], seq[-4] # 3 and -4
('D', 'D')
By looking at the correspondences above,
we can see that the positive index n
pairs up with the index -n - 1
.
Python has a couple of bitwise operations, one of them being bitwise invert ~
.
Bitwise invert ~n
is defined as -(n+1)
:
>>> n = 38
>>> ~n
-39
>>> -(n+1)
-39
>>> n = -73
>>> ~n
72
>>> -(n+1)
72
Now, maybe you can see where I'm going with this, but -(n+1)
simplifies to -n - 1
.
If we put these two pieces of knowledge together,
we can see how we can use bitwise inversion ~
to index symmetrically from the beginning
and end of a sequence, like a string or a list:
>>> seq = "abccba"
>>> seq[1], seq[~1]
('b', 'b')
>>> for i in range(len(seq) // 2):
... print(seq[i], seq[~i])
...
a a
b b
c c
Doesn't this look beautiful?
I feel like this is one of those things that you really won't use that often,
but there will come a time in your life when you'll want to exploit this symmetry!
And, you either remember what you just learned about ~
, or you'll be writing the uglier version with subtractions:
>>> seq = "abccba"
>>> seq[1], seq[~1]
('b', 'b')
>>> for i in range(len(seq) // 2):
... print(seq[i], seq[-i - 1])
...
a a
b b
c c
Thanks a lot to Tushar Sadhwani from bringing this to my attention:
🐍Python Tip of the day:
— Tushar Sadhwani (@sadhlife) November 28, 2021
You can use `~i` to index a list in python from behind, instead of `-1 - i`: pic.twitter.com/cVm4JE2aZA
That's it for now! Stay tuned and I'll see you around!
The next cohort of the Intermediate Python Course starts soon.
Grab your spot now and learn the Python skills you've been missing!
~
in indexing, https://twitter.com/sadhlife/status/1464993896346181637 [last consulted 28-11-2021];