Slicing [N::-1]
Mensanator
mensanator at aol.com
Fri Mar 5 19:52:28 CET 2010
On Mar 5, 12:28 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Fri, 05 Mar 2010 18:12:05 +0000, Arnaud Delobelle wrote:
> >>>> 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]
>
> Where does the first -1 come from? Slices are supposed to have default
> values of 0 and len(seq):
The only way to get a 0 from a reverse range() is to have a bound of
-1.
>
> >>> l[7::1]
> [7, 8, 9]
> >>> [l[i] for i in range(7, len(l), 1)]
> [7, 8, 9]
> >>> [l[i] for i in range(7, len(l), -1)]
>
> []
>
> I don't believe the actual behaviour is documented anywhere.
Well, it's implied. If the stopping bound in a reverse range()
is greater than the starting bound, you get an empty return.
>
> --
> Steven
More information about the Python-list
mailing list