Iterate over a List in reverse order in Python

avatar

Borislav Hadzhiev

Last updated: Aug 29, 2022

banner

Photo from Unsplash

Iterate over a List in reverse order in Python #

Use the reversed() function to iterate over a list in reverse order, e.g. for item in reversed(my_list):. The reversed() function takes an iterator, such as a list, reverses it and returns the result.

main.py
my_list = ['a', 'b', 'c', 'd'] # ✅ iterate over list in reverse order (reversed()) for item in reversed(my_list): print(item) # 👉️ d c b a # ---------------------------------- # ✅ iterate over list backwards (negative step) for item in my_list[::-1]: print(item) # 👉️ d c b a # ---------------------------------- # ✅ iterate over list in reverse order (with index) for index, item in reversed(list(enumerate(my_list))): print(index, item) # 👉️ 3 d 2 c 1 b 0 a

The first example uses the reversed() function to reverse a list.

The reversed function takes an iterator, reverses it and returns the result.

main.py
my_list = ['a', 'b', 'c', 'd'] reversed_list = list(reversed(my_list)) print(reversed_list) # 👉️ ['d', 'c', 'b', 'a']
The reversed() function returns an iterator object. You can pass it to the list() class if you need to convert the object to a list.

However, if you only need to iterate over the list in reverse order, you can can do it directly.

main.py
my_list = ['a', 'b', 'c', 'd'] for item in reversed(my_list): print(item) # 👉️ d c b a

Alternatively, you can use a negative step.

Iterate over a List in reverse order using a negative step #

Use a negative step to iterate over a list in reverse order, e.g. for item in my_list[::-1]:. The slice with a step of -1 is used to reverse the list before iterating over it with a for loop.

main.py
my_list = ['a', 'b', 'c', 'd'] for item in my_list[::-1]: print(item) # 👉️ d c b a

We used a negative step to reverse the list before iterating over it.

main.py
my_list = ['a', 'b', 'c', 'd'] # 👇️ ['d', 'c', 'b', 'a'] print(my_list[::-1])

The syntax for list slicing is my_list[start:stop:step].

We omitted the start and stop values and only specified a negative step to reverse the list.

Which approach you pick is a matter of personal preference. I'd go with using the reversed() function as I find it more readable and intuitive.

Iterate over a List in reverse order with Index #

To iterate over a list in reverse order with index:

  1. Use the enumerate() function to get access to the index.
  2. Convert the enumerate object to a list and reverse the list.
  3. Use a for loop to iterate over the list in reverse order.
main.py
my_list = ['a', 'b', 'c', 'd'] for index, item in reversed(list(enumerate(my_list))): print(index, item) # 👉️ 3 d 2 c 1 b 0 a

We used the enumerate function to get access to the index when iterating.

The enumerate function takes an iterable and returns an enumerate object containing tuples where the first element is the index and the second is the item.

main.py
my_list = ['apple', 'banana', 'melon'] for index, item in enumerate(my_list): print(index, item) # 👉️ 0 apple, 1 banana, 2 melon

We used the list() class to convert the enumerate object to a list because the first argument the reversed() function takes is a sequence.

main.py
my_list = ['a', 'b', 'c', 'd'] # 👇️ [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')] print(list(enumerate(my_list))) # 👇️ [(3, 'd'), (2, 'c'), (1, 'b'), (0, 'a')] print(list(reversed(list(enumerate(my_list)))))

The last step is to use a for loop to iterate over the list of tuples.

The first element in each tuple is the index and the second is the corresponding value.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.