How to Iterate List Backwards in Python
-
Use the
reversed()
Function to Traverse a List in Reverse Order in Python -
Use the
range()
Function to Traverse a List in Reverse Order in Python -
Use the
for
Loop to Traverse a List in Reverse Order in Python
This tutorial will discuss various methods available to traverse a list in reverse order in Python.
Use the reversed()
Function to Traverse a List in Reverse Order in Python
We can traverse a list in Python in reverse order by using the inbuilt reversed()
function available. The reversed()
function returns the reversed iteration of the sequence provided as input.
The reversed()
function accepts a single parameter, which is the sequence to be reversed. The sequence can be a tuple, string, list, range, etc.
For example,
x = ["my", "unlimited", "sadness"]
for i in reversed(x):
print(i)
Output:
my
sadness
unlimited
However, to also access the original index of the sequence, we can further use the enumerate()
function on our list before we pass it to the reversed()
function.
See the following code.
x = ["my", "unlimited", "sadness"]
for i, e in reversed(list(enumerate(x))):
print(i, e)
Output:
2 sadness
1 unlimited
0 my
Hence, we obtain the output with the original Index of the sequence. However, it is worth noting that enumerate()
returns a generator, and generators cannot be reversed. Therefore, it is vital to convert it to a list first.
Use the range()
Function to Traverse a List in Reverse Order in Python
Another method to traverse a sequence in reverse order in Python is by using the range
function available.
The range
function available in Python returns a sequence of numbers, starting from 0 by default, which auto increments by an additional 1 (by default).
The range
function accepts three different parameters - start
(optional), stop
(required), step
(optional). All three parameters accept an integer number as input.
See the following code.
x = ["my", "unlimited", "sadness"]
for i in range(len(x) - 1, -1, -1):
print(i, x[i])
Output:
3 sadness
2 unlimited
1 my
Hence, as shown above, we can traverse the sequence in reverse order with the original index of the sequence.
Use the for
Loop to Traverse a List in Reverse Order in Python
We can traverse a list in reverse order in Python by using the for
loop. It iterates over a sequence which can be list, tuple, string, etc.
We can use it to traverse a list in reverse order, as shown below.
x = ["my", "unlimited", "sadness"]
for item in x[::-1]:
print(item)
Output:
sadness
unlimited
my
Note that the [::-1]
in the code above only slices the list in reverse just for the loop. Hence, it does not alter or modify the array of data or list provided permanently.
Related Article - Python List
- How to Convert a Dictionary to a List in Python
- How to Remove All the Occurrences of an Element From a List in Python
- How to Remove Duplicates From List in Python
- How to Get the Average of a List in Python
- What Is the Difference Between List Methods Append and Extend
- How to Convert a List to String in Python