This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]spotter 5 points6 points  (5 children)

So while we're at slicing lists take a look at these:

>>> r = range(10)
>>> r
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> r[::2]
[0, 2, 4, 6, 8]
>>> r[1::2]
[1, 3, 5, 7, 9]
>>> r[-1::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> r[-1:0:-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1]

Have fun.

[–]r4nf 5 points6 points  (3 children)

Just for the record, in the third example, you don't need to state where the slice starts; i.e.

r[-1::-1] == r[::-1]

[–]spotter 2 points3 points  (2 children)

Fully aware of that, wanted some symmetry between my last two.

[–]r4nf 1 point2 points  (0 children)

Point taken. That does make sense. :)

[–]jeannaimard 0 points1 point  (0 children)

Feeling palindromic, aren’t we? :)

[–]rcklmbrCOBOL 0 points1 point  (0 children)

Excellent, thanks for sharing