iterating in reverse
Paul Rubin
phr-n2002b at NOSPAMnightsong.com
Fri Jan 17 04:14:27 EST 2003
jbperez808 at yahoo.com (Jonathan P.) writes:
> > It's not too elegant, but
> >
> > for j in range(len(x) - 1, -1, -1):
> > i = x[j]
> > print i
>
> This looks like the best bet so far short of using 2.3. One thing
> though, would using xrange instead of range be preferable?
Yes, xrange is preferable. And I think the 2.3 method makes a second
copy of the list, so the wish to not make a copy is unsatisfied.
You could always say:
for j in range(len(x)):
i = x[-j - 1]
print i
More information about the Python-list
mailing list