How to make a reverse for loop in python?
Fredrik Lundh
fredrik at pythonware.com
Sat Sep 20 12:37:14 EDT 2008
Alex Snast wrote:
> I'm new to python and i can't figure out how to write a reverse for
> loop in python
>
> e.g. the python equivalent to the c++ loop
>
> for (i = 10; i >= 0; --i)
use range with a negative step:
for i in range(10-1, -1, -1):
...
or just reverse the range:
for i in reversed(range(10)):
...
(the latter is mentioned in the tutorial, and is the second hit if you
google for "python reverse for loop")
</F>
More information about the Python-list
mailing list