Open In App

Reversing a List in Python

Last Updated : 15 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In this article, we are going to explore multiple ways to reverse a list. Python provides several methods to reverse a list using built-in functions and manual approaches. The simplest way to reverse a list is by using the reverse() method.

Let’s take an example to reverse a list using reverse() method.

The reverse() method reverses the elements of the list in-place and it modify the original list without creating a new list. This method is efficient because it doesn’t create a new list.

a = [1, 2, 3, 4, 5]

# Reverse the list in-place
a.reverse()
print(a)

Output
[5, 4, 3, 2, 1]

Let’s see other different methods to reverse a list.

Using List Slicing

Another efficient way to reverse a list is by using list slicing. This method creates a new list that contains the elements of the original list in reverse order.

a = [1, 2, 3, 4, 5]

# Create a new list that is a reversed list
# of 'a' using slicing
rev = a[::-1]

print(rev)

Output
[5, 4, 3, 2, 1]

Using the reversed()

Python’s built-in reversed() function is another way to reverse the list. However, reversed() returns an iterator, so it needs to be converted back into a list.

a = [1, 2, 3, 4, 5]

# Use reversed() to create an iterator
# and convert it back to a list
rev = list(reversed(a))
print(rev)

Output
[5, 4, 3, 2, 1]

Explanation:

  • The reversed() function returns an iterator, which is then converted into a list using the list() constructor.
  • Unlike reverse(), this does not modify the original list.

Using a Loop (creating new reversed list)

If we want to reverse a list manually, we can use a loop (for loop) to build a new reversed list. This method is less efficient due to repeated insertions and extra space required to store the reversed list.

a = [1, 2, 3, 4, 5]

# Initialize an empty list to store reversed element
res = []

# Loop through each item and insert
# it at the beginning of new list
for val in a:
    res.insert(0, val)
print(res)  

Output
[5, 4, 3, 2, 1]

Using a Loop (In-place reverse)

This method is an optimized version of the above method. We can use this method if we want to reverse the list in-place (without creating new list) using for loop.

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Define the start and end indices
start, end = 2, 6

# Reverse the elements from index 2 to 6
while start < end:
  
    # Swap elements at start and end
    a[start], a[end] = a[end], a[start]
    
    # Move the start index forward
    start += 1
    
     # Move the end index backward
    end -= 1

print(a)

Output
[1, 2, 7, 6, 5, 4, 3, 8, 9, 10]

Explanation:

  • Keep start & end pointers for the beginning and ending index of the reverse range respectively.
  • Swap the elements at the start and end pointers.
  • Increment start by 1 and decrement end by 1.
  • Repeat step 2 and 3, until start pointer is smaller than end pointer.

Using List Comprehension

We can also use list comprehension to reverse the list, although this is less common than the other methods mentioned.

a = [1, 2, 3, 4, 5]

# Use list comprehension to create
# a reversed version of the list
rev = [a[i] for i in range(len(a) - 1, -1, -1)]
print(rev)

Output
[5, 4, 3, 2, 1]

Explanation:

  • range(len(a) – 1, -1, -1) goes through the list indices from the last to the first.
  • [a[i] for i in range(…)] collects elements in reverse order.

Which Method to Choose?

  • reverse(): Use for in-place modification when we don’t need the original list
  • List Slicing ([::-1]): Use to quickly create a reversed copy without modifying the original list
  • reversed(): Ideal for creating an iterator to reverse without modifying the original list and if we need an iterable for further operations.
  • Loop (In-Place): Use for more control during in-place reversal and especially if additional conditions are involved.

Avoid using list comprehensions or loops that create new lists because they are less efficient and more complex.
The reverse() method or list slicing ([::-1]) are generally recommended for their simplicity and efficiency.



Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg