Python: Reverse a List (6 Easy Ways)

Python Reverse a List Cover Image

In this tutorial, you’ll learn how to use Python to reverse a list. Python comes with a number of methods and functions that allow you to reverse a list, either directly or by iterating over the list object. You’ll learn how to reverse a Python list by using the reversed() function, the .reverse() method, list indexing, for loops, list comprehensions, and the slice method.

While learning six different methods to reverse a list in Python may seem excessive, one of the beautiful things about Python is the flexibility it affords. Some methods will fit better into your style, while others may be more performant or easier to read.

Let’s get started!

The Quick Answer: Use a Python’s .reverse()

Quick Answer - Python Reverse a List

How does list indexing work in Python?

One of the attributes of Python lists is that they are ordered and indexed. This means that we can access (and modify) a list based on its index.

The first item in a Python list will have an index position of 0. The last item, meanwhile, will have the index position of the length of the list minus 1. Alternatively, we can access the last item by its negative index. The last item has a negative index of -1 and decreases from there.

How does Python List Indexing Work

In the next section, you’ll learn how to use the Python reversed() function to reverse a list in Python.

Want to learn how to use the Python zip() function to iterate over two lists? This tutorial teaches you exactly what the zip() function does and shows you some creative ways to use the function.

Reverse a Python List Using the reversed function

Python comes with a function, reversed(), which takes an iterable object and returns the, well, reversed version of it. The reversed() method works similar to the list indexing method covered below and was added to Python to replicate the method without being overly verbose.

Let’s take a look at how we can use this method to reverse a Python list:

# Reverse a Python list with reversed()

original_list = [1,2,3,4,5,6,7,8,9]
reversed_list = list(reversed(original_list))

print(reversed_list)

# Returns: [9, 8, 7, 6, 5, 4, 3, 2, 1]

The reversed() function actually returns an iterator object, of type reverseiterator. We can verify this by writing the following code:

print(type(reversed(original_list))

# Returns: <class 'list_reverseiterator'>

Because of this, we need to use the list() function to create a list out of the iterator object. Similarly, we could instantiate a separate list and iterate over the iterator object, as you’ll learn later in the tutorial.

Want to learn more about Python f-strings? Check out my in-depth tutorial, which includes a step-by-step video to master Python f-strings!

Reverse a Python List Using the reverse method

Similar to the method above, Python offers a method that can act directly on a list, the .reverse() method.

The method works in-place, meaning there is no need to re-assign the list. If you do want to assign the old list to a new list, you’ll need to first create a copy of the original list.

Let’s see how we can reverse our Python list using the .reverse() method.

# Reverse a Python list with reverse()

original_list = [1,2,3,4,5,6,7,8,9]
original_list.reverse()
print(original_list)

# Returns: [9, 8, 7, 6, 5, 4, 3, 2, 1]

If we did want to assign the reversed list to a new variable while keeping the original, we will need to write a bit more code. Let’s take a look at how this works:

# Reverse a Python list with reverse()

original_list = [1,2,3,4,5,6,7,8,9]
original_list_copy = original_list.copy()

original_list_copy.reverse()
reversed_list = original_list_copy.copy()

print(f'{original_list=}')
print(f'{reversed_list=}')

# Returns:
# original_list=[1, 2, 3, 4, 5, 6, 7, 8, 9]
# reversed_list=[9, 8, 7, 6, 5, 4, 3, 2, 1]

In the next section, you’ll learn how to reverse a list by using list indexing.

Need to check if a key exists in a Python dictionary? Check out this tutorial, which teaches you five different ways of seeing if a key exists in a Python dictionary, including how to return a default value.

Reverse a Python List Using List Indexing

When indexing a list, people generally think of accessing all items between a start and an end position. However, you can also include a step variable that allows you to move across indices at different rates.

For example, a_list[::1] would return the entire list from beginning to end. If we, instead, stepped using a value of -1, then we would traverse the list from the last item to the first.

Moving over a list from end to beginning is the same as reversing it. Let’s see how we can use Python’s list indexing to reverse a list:

# Reverse a Python list with list indexing

original_list = [1,2,3,4,5,6,7,8,9]
reversed_list = original_list[::-1]
print(reversed_list)

# Returns: [9, 8, 7, 6, 5, 4, 3, 2, 1]

We return a new list here by iterating over each item in a reverse order, thereby reversing our list.

In the next section, you’ll learn how to use the .slice() method.

Need to check if a key exists in a Python dictionary? Check out this tutorial, which teaches you five different ways of seeing if a key exists in a Python dictionary, including how to return a default value.

Reverse a Python List Using the Slice Method

Python also provides a slice() function that allows us to index an object in the same way that the list indexing method works above. The function creates a slice object that allows us to easily reuse a slice across multiple objects. Because of this, we could apply the same indexing across different lists.

Let’s see how we can use the function in Python:

# Reverse a Python list with slice()

original_list = [1,2,3,4,5,6,7,8,9]
slicer = slice(None, None, -1)
reversed_list = original_list[slicer]
print(reversed_list)

# Returns: [9, 8, 7, 6, 5, 4, 3, 2, 1]

The benefit of this approach is that we can assign a custom slice that can be easily re-used, without needing to modify multiple items.

In the next section, you’ll learn how to use a for loop to return a list in the opposite order.

Check out some other Python tutorials on datagy, including our complete guide to styling Pandas and our comprehensive overview of Pivot Tables in Pandas!

Reverse a Python List Using a For Loop

Python for loops are great ways to repeat an action for a defined number of times. We can use the reversed() and iterate over its items to create a list in the reverse order.

Let’s see how this can be done:

# Reverse a Python list with a for loop

original_list = [1,2,3,4,5,6,7,8,9]
reversed_list = list()

for item in reversed(original_list):
    reversed_list.append(item)
print(reversed_list)

# Returns: [9, 8, 7, 6, 5, 4, 3, 2, 1]

This method is actually very similar to what we do with the reversed() function method above. However, if the result of the function isn’t clear to readers, this result may actually be easier to follow.

In the final section of this tutorial, you’ll learn how to use a list comprehension to return a Python list in the opposite order.

Want to learn more about Python for-loops? Check out my in-depth tutorial that takes your from beginner to advanced for-loops user! Want to watch a video instead? Check out my YouTube tutorial here.

Reverse a Python List Using a List Comprehension

Similar to how we can use a for loop for this, we can also use a list comprehension. Rather than simply converting the for loop to a list comprehension, however, we’ll use a slightly different approach to accomplish our goal.

We will iterate over each item in our original list in the negative order. Let’s take a look at what this looks like:

# Reverse a Python list with a list comprehension

original_list = [1,2,3,4,5,6,7,8,9]
starting_index = len(original_list) - 1
reversed_list = [original_list[i] for i in range(len(original_list)-1, 0, -1)]
print(reversed_list)

# Returns: [9, 8, 7, 6, 5, 4, 3, 2, 1]

In this list comprehension, we access each index from the last to the first, decreasing by 1. We start out range using the max index (which is equal to the length of the original list, minus 1).

Want to learn more about Python list comprehensions? Check out this in-depth tutorial that covers off everything you need to know, with hands-on examples. More of a visual learner, check out my YouTube tutorial here.

Conclusion

In this tutorial, you learned how to reverse a list in Python. You learned how list indexing works and how this can be used to reverse a list. You also learned how to use the reverse() and reversed() methods, as well as for loops and list comprehensions.

To learn more about lists in Python, check out the official documentation here.