Iterate / traverse a list in reverse order in Python

Traverse a Python list in reverse order: In this tutorial, we will learn how to iterate/traverse a given Python list in reverse order using multiple approaches and examples. By IncludeHelp Last updated : June 22, 2023

Given a Python list, we have to traverse it in reverse order using multiple approaches. Consider the below example with sample input and output -

Input:
List = [10, 20, 30, 40, 50]
Output:
list = [50, 40, 30, 20, 10]

Input:
list = ['Hello', 10 'World', 20]
Output:
list = [20, 'World', 10, 'Hello']

Traverse a Python list in reverse order

There can be multiple approaches to traverse a Python list in reverse order. Some of the approaches are as follows:

Let's discuss each approach in details:

1. Using list slicing

The easiest and simplest approach for traversing a Python list is that - You can use list slicing. Use negative indexing i.e., ::-1. The statement list[::-1] will return the list in reverse order.

Example

# Traverse a Python list in reverse order
# Using list slicing

# Define a list
list1 = [10, 20, 30, 40, 50]

# Print the list
print("Original list: ", list1)

# Reverse the list
newlist = list1[::-1]

# print the list
print("List in reverse order: ", newlist)

# another list with string and integer elements
list2 = ["Hello", 10, "world", 20]

# print the list
print("Original list: ", list2)

# Reverse the list
newlist = list2[::-1]

# print the list
print("List in reverse order: ", newlist)

Output

Original list:  [10, 20, 30, 40, 50]
List in reverse order:  [50, 40, 30, 20, 10]
Original list:  ['Hello', 10, 'world', 20]
List in reverse order:  [20, 'world', 10, 'Hello']

2. Using reversed() method

You can also use reversed() method while iterating over the list elements. It returns a reverse iterator and it does not modify the given list.

Example

# Traverse a Python list in reverse order
# Using reversed() method

# Define a list
list1 = [10, 20, 30, 40, 50]

# Print the list
print("Original list: ", list1)

# Traverse list in reverse order
print("List in reverse order:")
for item in reversed(list1):
    print(item)

Output

Original list:  [10, 20, 30, 40, 50]
List in reverse order:
50
40
30
20
10

3. Using iterate list elements and insert() method

Another approach is that - Iterate over the list using for ... in loop and insert each element at the 0th index using the list.insert() method. When a new element will be added at the 0th index the previous element will be shifted to the next position. In this way, we will get a reversed list.

Example

# Traverse a Python list in reverse order
# Using iterate list elements and 
# insert() method

# Define a list
list1 = [10, 20, 30, 40, 50]

# Print the list
print("Original list: ", list1)

# Create a new list with elements 
# in reverse order
newlist = [] # an empty list
for item in list1:
    newlist.insert(0, item)

# Print the new list
print("List in reverse order:",newlist)

Output

Original list:  [10, 20, 30, 40, 50]
List in reverse order: [50, 40, 30, 20, 10]

Python List Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.