# Python array indexing fun

```
>>> l = list(range(10))
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l[-1]
9
>>> l[~0]
9
>>> l[~1]
8
>>> l[~2]
7
>>> l[-0]
0
```

Python has two ways to index arrays (in python called Lists) from the end. You can use a minus sign, starting at 1, or a tilde, starting at 0. Nice! I will be using this a lot from now on, it avoids bugs where you index -0 as in the last example above.

What’s happening behind the scenes here is that the ~ operator on an integer inverts all of the bits (making the number negative) and subtracts 1. A handy coincidence.

From the python docs:

The unary

`~`

(invert) operator yields the bitwise inversion of its integer argument. The bitwise inversion of`x`

is defined as`-(x+1)`

. It only applies to integral numbers.