Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop backwards using indices in Python?

Tags:

python

loops

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.

like image 897
Joan Venge Avatar asked May 15 '09 17:05

Joan Venge


People also ask

Can you loop backwards in Python?

To reverse for loop in Python just need to read the last element first and then the last but one and so on till the element is at index 0. You can do it with the range function, List Comprehension, or reversed() function.


1 Answers

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

("range" options, start, stop, step are documented here)

like image 174
0x6adb015 Avatar answered Oct 06 '22 00:10

0x6adb015