Last updated: Apr 9, 2024
Reading timeยท4 min
To iterate over a list in reverse order with index:
enumerate()
function to get access to the index.enumerate
object to a list and reverse the list.for
loop to iterate over the list in reverse order.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.
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.
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.
Use the reversed()
function to iterate over a list in reverse order.
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.
my_list = ['a', 'b', 'c', 'd'] reversed_list = list(reversed(my_list)) print(reversed_list) # ๐๏ธ ['d', 'c', 'b', 'a']
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.
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.
The slice with a step of -1
is used to reverse the list before iterating over
it with a for
loop.
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.
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]
.
start
and stop
values and only specified a negative step to reverse the list.You can also use the reversed()
function to iterate over a string in reverse
order.
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.
my_str = 'abcd' for char in reversed(my_str): print(char) # ๐๏ธ d, c, b, a
Alternatively, you can use a negative step.
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.
my_str = 'abcd' reversed_str = my_str[::-1] print(reversed_str) # ๐๏ธ dcba
The syntax for string slicing
is my_string[start:stop:step]
.
start
and stop
values and only specified a negative step to reverse the string.To iterate over a string backward with index:
enumerate()
function to get access to the index.enumerate
object to a list and reverse the list.for
loop to iterate over the string in reverse order.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.
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.
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.
You can learn more about the related topics by checking out the following tutorials: