Slicing [N::-1]

Shashwat Anand anand.shashwat at gmail.com
Fri Mar 5 13:33:21 EST 2010


On Fri, Mar 5, 2010 at 11:42 PM, Arnaud Delobelle <arnodel at googlemail.com>wrote:

> 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]
>

>>> 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]
>>> list(reversed(l))[-8:]
[7, 6, 5, 4, 3, 2, 1, 0]

~l0nwlf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100306/14fb7502/attachment-0001.html>


More information about the Python-list mailing list