Iterate through a list in reverse order in Python

In this tutorial, we are going to see how to iterate through a list in reverse order in Python. Unlike arrays, lists can store elements of different data types. Other collective data types in Python are Tuple, Set, and Dictionary.

First of all, define the list of some elements.

l = [1, 2, 3.5, "ABC", 'x', 5]

reversed( ) function

reversed() is a built-in function in Python that reverses the sequence and returns it in form of a list.
Now pass the list to the function and traverse using a loop. Example: for loop, while loop, etc. This will first reverse the list and iterate through it.

for i in reversed(l):
    print(i,end=" ")

This will access elements from the list in a backward manner.

5 x ABC 3.5 2 1

Also, see Backward Iteration in Python

Leave a Reply

Your email address will not be published. Required fields are marked *