Slicing [N::-1]

Arnaud Delobelle arnodel at googlemail.com
Fri Mar 5 13:12:05 EST 2010


Joan Miller <peloko45 at gmail.com> writes:

> What does a slice as [N::-1] ?
>
> It looks that in the first it reverses the slice and then it shows
> only N items, right?
>
> Could you add an example to get the same result without use `::` to
> see it more clear?
>
> Thanks in advance

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

-- 
Arnaud



More information about the Python-list mailing list