Loop backwards using indices in Python?


Question

I am trying to loop from 100 to 0. How do I do this in Python?

for i in range (100,0) doesn't work.

1
237
5/15/2009 5:17:06 PM

Accepted Answer

Try range(100,-1,-1), the 3rd argument being the increment to use (documented here).

339
8/23/2014 5:55:31 PM

In my opinion, this is the most readable:

for i in reversed(xrange(101)):
    print i,

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon