Try range(100,-1,-1), the 3rd argument being the increment to use (documented here).
("range" options, start, stop, step are documented here)
Answer from 0x6adb015 on Stack OverflowWhy isn't my loop iterating backwards in python?
How to Loop Backwards in Python? - TestMu AI Community
python - Moving back an iteration in a for loop - Stack Overflow
How can I loop through an array forward and then backwards and then forwards and... etc with the modulus operator?
Videos
range() and xrange() take a third parameter that specifies a step. So you can do the following.
range(10, 0, -1)
Which gives
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
But for iteration, you should really be using xrange instead. So,
xrange(10, 0, -1)
Note for Python 3 users: There are no separate
rangeandxrangefunctions in Python 3, there is justrange, which follows the design of Python 2'sxrange.
for x in reversed(whatever):
do_something()
This works on basically everything that has a defined order, including xrange objects and lists.
Basically, I wanted the loop to start from the last index and work that way done. However, I am not getting anything.
listr = [10,20,30,50]
count = 0
for i in range(len(listr),-1):
count +=1
print(listr[i], count)
print('-----')for loops in Python always go forward. If you want to be able to move backwards, you must use a different mechanism, such as while:
i = 0
while i < 5:
print(i)
if condition:
i=i-1
i += 1
Or even better:
i = 0
while i < 5:
print(i)
if condition:
do_something()
# don't increment here, so we stay on the same value for i
else:
# only increment in the case where we're not "moving backwards"
i += 1
Python loop using range are by-design to be different from C/C++/Java for-loops. For every iteration, the i is set the the next value of range(5), no matter what you do to i in between.
You could use a while-loop instead:
i = 0
while i<5:
print i
if condition:
continue
i+=1
But honestly: I'd step back and think again about your original problem. Probably you'll find a better solution as such loops are always error-prone. There's a reason why Python for-loops where designed to be different.
Say I have an array:
arr = [1,2,3,4,5]
and I want to output this using a simple counter and the modulus operator
1 2 3 4 5 4 3 2 1 2 3 4 5 ...
Is this possible? I know that you can loop through an array and start over at the beginning by doing
arr[count % len(arr)]
but how do I just switch directions instead of going back to the beginning?
I want to iterate over a string in reverse. I googled it and found this way
for i in range(len(k)-1, 0-1, -1):
but to be honest I don't understand it. can you please explain it to me?
also, I would love it if you guys can suggest other methods
Use the built-in reversed() function:
>>> a = ["foo", "bar", "baz"]
>>> for i in reversed(a):
... print(i)
...
baz
bar
foo
To also access the original index, use enumerate() on your list before passing it to reversed():
>>> for i, e in reversed(list(enumerate(a))):
... print(i, e)
...
2 baz
1 bar
0 foo
Since enumerate() returns a generator and generators can't be reversed, you need to convert it to a list first.
You can do:
for item in my_list[::-1]:
print item
(Or whatever you want to do in the for loop.)
The [::-1] slice reverses the list in the for loop (but won't actually modify your list "permanently").