Python : Different ways to Iterate over a List in Reverse Order

In this article we will discuss different ways to Iterate over a python list in reverse order.

Suppose we have a python list of strings i.e.

# List of string
wordList = ['hi', 'hello', 'this', 'that', 'is', 'of']

Now we want to iterate over this list in reverse order( from end to start ) i.e.
of
is
that
this
hello
hi

We don’t want to change the order in existing list, just want to iterate in reverse. Now let’s see how to do this using different techniques,

Iterate over the list in reverse using while loop

Get the size of list and using random and use random access operator [] to access elements in reverse i.e. from (size-1) to 0.

'''
Iterate over the list in reverse using while loop
'''
# Point i to the last element in list
i = len(wordList) - 1

# Iterate till 1st element and keep on decrementing i
while i >= 0 :
print(wordList[i])
i -= 1

Iterate over the list in reverse using for loop and range()

Suppose if wordList had n elements then,

range( len(wordList) - 1, -1, -1)

Will return list of numbers from n to 1

For example, wordList had 5 elements then above specified range() function will return,

4, 3, 2 , 1, 0

Now use that range() function in for loop and use random access operator [] to access elements in reverse i.e.

'''
Iterate over the list using for loop and range()
'''
for i in range( len(wordList) - 1, -1, -1) :
print(wordList[i])

Iterate over the list using for loop and reversed()

reversed(wordList)

reversed() function returns an iterator to accesses the given list in the reverse order.

Let’s iterate over that reversed sequence using for loop i.e.

'''
Iterate over the list using for loop and reversed()
'''
for i in reversed(wordList) :
print(i)

It will print the wordList in reversed order.

Iterate over the list using List Comprehension and [::-1]

wordList[::-1]

It will create a temporary revesed list

Let’s use this in List comprehension to iterating over the list in reverse i.e.

'''
Iterate over the list using List Comprehension and [::-1]
'''
[print (i) for i in wordList[::-1]]

Iterate over the list using List Comprehension and reversed()

'''
Iterate over the list using List Comprehension and [::-1]
'''
[print (i) for i in reversed(wordList)]

Complete example is as follows,
"""
Different ways to Iterate over a list in reverse Order
"""

def main():

# List of string
wordList = ['hi', 'hello', 'this', 'that', 'is', 'of']

#print the List
print(wordList)

'''
Iterate over the list in reverse using while loop
'''
# Point i to the last element in list
i = len(wordList) - 1

# Iterate till 1st element and keep on decrementing i
while i >= 0 :
print(wordList[i])
i -= 1

print("*************")

'''
Iterate over the list using for loop and range()
'''
for i in range( len(wordList) - 1, -1, -1) :
print(wordList[i])

print("*************")

'''
Iterate over the list using for loop and reversed()
'''
for i in reversed(wordList) :
print(i)

print("*************")

'''
Iterate over the list using List Comprehension and [::-1]
'''
[print (i) for i in wordList[::-1]]

print("*************")

'''
Iterate over the list using List Comprehension and [::-1]
'''
[print (i) for i in reversed(wordList)]

if __name__ == "__main__":
main()

Output:
['hi', 'hello', 'this', 'that', 'is', 'of']
of
is
that
this
hello
hi
*************
of
is
that
this
hello
hi
*************
of
is
that
this
hello
hi
*************
of
is
that
this
hello
hi
*************
of
is
that
this
hello
hi