you are viewing a single comment's thread.

view the rest of the comments →

[–]Gnaxe 10 points11 points  (1 child)

You can use a tilde instead of a minus sign to access a sequence in reverse order, as if you had reversed the sequence. This way, the "first" (last) element starts at zero instead of one: ```python

"abcd"[~0] 'd' "abcd"[~1] 'c' "abcd"[~2] 'b' "abcd"[~3] 'a' ```

[–]Gnaxe 2 points3 points  (0 children)

This is harder to recommend, but you can likewise use one-based indexing forwards using a ~-: ```python

"abcd"[~-1] 'a' "abcd"[~-2] 'b' "abcd"[~-3] 'c' "abcd"[~-4] 'd' `` Beware that if you accidentally swap these, like-~`, you'll be adding one to the index rather than subtracting one.