iterating in reverse

Andrew Bennetts andrew-pythonlist at puzzling.org
Thu Jan 16 19:30:30 EST 2003


On Thu, Jan 16, 2003 at 03:46:32PM -0800, Jonathan P. wrote:
> 
> will always print x's contents in the
> same sequence.  Is there an elegant and
> efficient way to iterate through x in
> reverse without having to create a reversed
> copy of it (i.e. y=x[:]; y.reverse())?

Get the Python 2.3 alpha, and it's really easy :)

>>> l = [1,2,3,4,5]
>>> for i in l[::-1]:
...     print i
... 
5
4
3
2
1

-Andrew.






More information about the Python-list mailing list