How to make a reverse for loop in python?

Fredrik Lundh fredrik at pythonware.com
Sat Sep 20 12:46:35 EDT 2008


Fredrik Lundh wrote:

>> 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)):
>         ...

(and to include the 10 in the range, add one to the 10 above)

</F>




More information about the Python-list mailing list