Iterate over a List or String in Reverse order in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
4 min

banner

# Table of Contents

  1. Iterate over a List in reverse order with Index
  2. Iterate over a List in reverse order
  3. Iterate over a List in reverse order using a negative step
  4. Iterate over a String backward in Python
  5. Iterate over a string backward using a negative step
  6. Iterate over a string backward with index

# 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

iterate over list in reverse order with index

The code for this article is available on GitHub

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.

# Iterate over a List in reverse order in Python

Use the reversed() function to iterate over a list in reverse order.

main.py
my_list = ['a', 'b', 'c', 'd'] for item in reversed(my_list): print(item) # ๐Ÿ‘‰๏ธ d c b a

iterate over list in reverse order

The code for this article is available on GitHub

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

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

Alternatively, you can use a negative step.

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

iterate over list in reverse order using negative step

The code for this article is available on GitHub

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.

# Iterate over a string backward in Python

You can also use the reversed() function to iterate over a string in reverse order.

main.py
my_str = 'abcd' # โœ… Iterate over a string in reverse order (reversed()) for char in reversed(my_str): print(char) # ๐Ÿ‘‰๏ธ d, c, b, a

iterate over string backward

The code for this article is available on GitHub

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

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

main.py
my_str = 'abcd' for char in reversed(my_str): print(char) # ๐Ÿ‘‰๏ธ d, c, b, a

# Iterate over a string backward using a negative step

Alternatively, you can use a negative step.

main.py
my_str = 'abcd' for char in my_str[::-1]: print(char) # ๐Ÿ‘‰๏ธ d, c, b, a

iterate over string backward using negative step

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

main.py
my_str = 'abcd' reversed_str = my_str[::-1] print(reversed_str) # ๐Ÿ‘‰๏ธ dcba

The syntax for string slicing is my_string[start:stop:step].

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

# Iterate over a string backward with index

To iterate over a string backward 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 string in reverse order.
main.py
my_str = 'abcd' for index, char in reversed(list(enumerate(my_str))): print(index, char) # ๐Ÿ‘‰๏ธ 3 d, 2 c, 1 b, 0 a
The code for this article is available on GitHub

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_str = 'abcd' for index, char in enumerate(my_str): print(index, char) # ๐Ÿ‘‰๏ธ 0 a, 1 b, 2 c, 3 d

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_str = 'abcd' # ๐Ÿ‘‡๏ธ [(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')] print(list(enumerate(my_str))) # ๐Ÿ‘‡๏ธ [(3, 'd'), (2, 'c'), (1, 'b'), (0, 'a')] print(list(reversed(list(enumerate(my_str)))))

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 character.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev