Backward list indices
Nicola Larosa
googlegroups at tekNico.net
Thu Dec 19 18:14:56 EST 2002
I need to loop backwards over a list's items.
I searched the archives and found a few ways, using the "range"
function with a negative step, or going forward and then "inverting"
the index.
Those ways work, but the thing that kind of surprised me was this
excerpt from the docs:
"The slice of s from i to j is defined as the sequence of items with
index k such that i <= k < j. [...] If i is greater than or equal to
j, the slice is empty."
Why is that so? It would seem natural that, if i is greater than or
equal to j, the slice is populated with the items in backward order,
be it myList[5:3] or myList[-3:-7] .
Is it costly implementation-wise? Or maybe there's fear of confusing
matters?
I beg your expert advice. :^)
More information about the Python-list
mailing list