A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-reversing-list/ below:

Reversing a List in Python

Reversing a List in Python

Last Updated : 15 Oct, 2024

Try it on GfG Practice

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.

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

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

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.

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

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

print(rev)
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.

Python
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)

Explanation:

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.

Python
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)  
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.

Python
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:

Using List Comprehension

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

Python
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)

Explanation:

Which Method to Choose?

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.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4