I would like to loop through a list in reverse order in Python. I get it in C#, Java, etc but not in Python.
This is what I have:
lista = [100, 200, 500, 2, 36]
n = len(lista)
for i in len(lista):
print(lista[n])
n = n-1
Worse I get an error on the line print(lista[n])
: it won't let me pass "n" as the index of the list.
Simply use the reversed() function , which comes standard with Python:
For your list, return:
About your code and why it doesn't work:
First of all, if you save the value of the length of the list, use that variable!
Also, you are iterating over
len(lista)
, that is, you are trying to iterate over a single element, since itlen(lista)
is an integer. It's like sayingfor i in 5
, which will make you iterate only once.Therefore, what you probably want to do is go through a range of data that goes from 0 to that length.
Then, it is about printing each time an element of position -1 with respect to the previous one.
All together, it would look like this:
Or, more clearly and as FJSevilla proposes in the comments , use a negative feed to start from the last element and thus have a clearer index:
In addition to solutions based on
reversed()
another commonly seen trick is to use the slice syntax , commonly used to extract a subset of a list, as inlista[inicio:fin]
.The slice operator supports a third parameter, which is the increment between indices. For example, to pull elements every 2 indices:
lista[inicio:fin:2]
. The increment can be negative, in which caseinicio
it must be greater thanfin
and the elements are traversed backwards. When the increment is negative, if is omittedinicio
, the end of the list is assumed and if omittedfin
, the beginning is assumed.Putting all this together results
lista[::-1]
in the list being returned in reverse order. So that:Generally in python, you can use negative indices to start from the back.
result:
A little magic can be done with the indices:
Departure:
Another possible solution is to first get the list in reverse order, which is easy to do using the reversed function. You can then iterate through that new list normally. It should be noted that this method is recommended when the size of the lists you manage is between small and medium (a few thousand), otherwise the creation of a new list can consume significant time and resources. On the other hand, using this approach is generally more readable and less error-prone.
Example, if you have a list:
And you do L2 = reversed(L) you get
Then you can do the print normally like:
The above code works in python 3 but you can implement the same thing in python2 like:
Another way to do it would be:
exit.
A help to your code can be
or another way is:
one more with the original code, adding range and exchanging two lines
36 2 500 200 100
[36, 2, 500, 200, 100]
36 2 500 200 100
You must subtract one an, since it gives the error out of range
I am very new to Python, but to go through a list backwards, it is much easier to use a while loop, look... (also with for, but I see it more abstractly)