# Iterate over a List or String in Reverse order in Python

Last updated: Apr 9, 2024
4 min

## #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
```Copied!```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
```Copied!```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
```Copied!```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
```Copied!```my_list = ['a', 'b', 'c', 'd']

for item in reversed(my_list):
print(item)  # ๐๏ธ d c b 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
```Copied!```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
```Copied!```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
```Copied!```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
```Copied!```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
```Copied!```my_str = 'abcd'

# โ Iterate over a string in reverse order (reversed())

for char in reversed(my_str):
print(char)  # ๐๏ธ d, c, b, a
``````

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
```Copied!```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
```Copied!```my_str = 'abcd'

for char in my_str[::-1]:
print(char)  # ๐๏ธ d, c, b, a
``````

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

main.py
```Copied!```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
```Copied!```my_str = 'abcd'

for index, char in reversed(list(enumerate(my_str))):
print(index, char)  # ๐๏ธ 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
```Copied!```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
```Copied!```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.