Slice objects with negative increment
Alex Martelli
aleax at aleax.it
Wed May 1 03:50:58 EDT 2002
Huaiyu Zhu wrote:
...
> Summarization of the rules left as an exercise to the reader. :-)
> When the bounds are out of range, the rules are not very obvious, in fact.
Funny, I find them clear. For a sequence of length L, valid indices are
-len(L) included to len(L) excluded (any index n<0 indicates just the same
item as n+len(L)). In slicing, an index "off to the left", i.e. some
n < -len(L), is equivalent to "the first item included"; an index "off
to the right", i.e. some n >= len(L) is equivalent to "the last item
included". This is just the same whether the slice's stride is <0 or >0.
Alex
More information about the Python-list
mailing list