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.
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").
Hello - why is this code not working an i get an error:
l = [1,2,3] for i in l.reverse(): print(i)
and i get this error
Traceback (most recent call last):
File "C:\Users\Polzi\Documents\DEV\Python-Private\temp1.py", line 7, in <module>
for i in l.reverse():
TypeError: 'NoneType' object is not iterable
(NormalScraping) Only this code is working:
l = [1,2,3] l.reverse() for i in l: print(i)
Why is the first code not working?
python - How do I reverse a list or loop over it backwards? - Stack Overflow
How do I use reverse list in Python to loop through a list backwards? - Ask a Question - TestMu AI Community
Differences in iteration through a loop in reverse order - Python
loops - How to traverse a list in reverse order in Python (index-style: '... in range(...)' only) - Stack Overflow
Videos
To get a new reversed list, apply the reversed function and collect the items into a list:
>>> xs = [0, 10, 20, 40]
>>> list(reversed(xs))
[40, 20, 10, 0]
To iterate backwards through a list:
>>> xs = [0, 10, 20, 40]
>>> for x in reversed(xs):
... print(x)
40
20
10
0
>>> xs = [0, 10, 20, 40]
>>> xs[::-1]
[40, 20, 10, 0]
Extended slice syntax is explained here. See also, documentation.
So I'm building a game that requires me to loop through a list in reverse order.
It seems that there are multiple ways to do this same task, but I want to make sure I understand the differences between the alternative ways.
From what I have found online:
I can use list comprehension -
for _ in desired_list[::-1]
I can use reversed() -
for _ in revered(desired_list)
I can use a range() -
for _ in range(len(desired_list) -1, 0, -1)
Is there any functional difference between these?
Also for the range iteration, could you verify what each number stands for?
It seems to be:
range("number to start on", "number to end up", "direction and distance to travel in list")
Is that correct?
Thank you for any help!
Avoid looping with range, just iterate with a for loop as a general advice.
If you want to reverse a list, use reversed(my_list).
So in your case the solution would be:
my_list = reversed([array0,array1,array2,array3])
That gives you [array3, array2, ...]
For omitting the last array from the reversed list, use slicing:
my_list = reversed([array0,array1,array2,array3])[:-1]
So your code turns to a one liner :-)
You can traverse a list in reverse like so:
for i in range(len(lis), 0, -1):
print (i)
To stop at the second element edit your range() function to stop at 1:
for i in range(len(lis), 1, -1):
print (i)