How to make a reverse for loop in python?

Peter Otten __peter__ at web.de
Sat Sep 20 12:47:22 EDT 2008


Gary Herron wrote:

> Or you can create a new reversed (copy of the original) list and iterate 
> through it
> 
> for item in reversed(L):
>   print item

It's not a copy, it's a view:

>>> items = [1,2,3]
>>> r = reversed(items)
>>> items[:] = "abc"
>>> for item in r: print item
...
c
b
a

Peter



More information about the Python-list mailing list