In this tutorial, you’ll learn how to use Python to reverse a list. This is a common task at hand (and a common Python interview question you’ll encounter). Knowing how to approach this task in different ways can help you choose the right method in different situations.
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()
# Using list.reverse() to reverse a list in Python
values = [1, 2, 3, 4, 5]
values.reverse()
print(values)
# Returns: [5, 4, 3, 2, 1]
Table of Contents
Summary: How to Reverse a List in Python
In this guide, we’ll cover six different ways of reversing a list in Python. However, let’s cover what the best and fastest way to reverse a list in Python is. From there, you can read about the various methods in the sections below.
The most Pythonic way to reverse a list is to use the .reverse()
list method. This is because it’s intentional and leaves no doubt in the reader’s mind as to what you’re hoping to accomplish.
The fastest way to reverse a list in Python is to use either a for loop or a list comprehension. In order to test this, we tested each method on a list containing 100,000,000 items! This allowed us to really stress test each of the different methods.
Let’s take a look at what the results were for each of the methods:
Method | Time to reverse a 100,000,000 item list |
---|---|
.reverse() method | 32.6 ms ± 3.35 ms per loop |
reversed() function | 680 ms ± 21.6 ms per loop |
Reverse indexing | 386 ms ± 3.14 ms per loop |
For loop | 751 ns ± 1.83 ns per loop |
List comprehension | 747 ns ± 1.12 ns per loop |
Slicer object | 382 ms ± 2.35 ms per loop |
Now that we’ve covered that, let’s dive into how to use these different methods to reverse a list in Python.
Understanding Python List Indexing
Before we dive into how to reverse a Python list, it’s important for us to understand how Python lists are indexed. Because Python lists are ordered and indexed, we can access and modify Python lists based on their index.
Python list indices work in two main ways:
- Positive Index: Python lists will start at a position of
0
and continue up to the index of the length minus 1 - Negative Index: Python lists can be indexed in reverse, starting at position
-1
, moving to the negative value of the length of the list.
The image below demonstrates how list items can be indexed.
In the next section, you’ll learn how to use the Python list .reverse()
method to reverse a list in Python.
Reverse a Python List Using the reverse method
Python lists have access to a method, .reverse()
, which allows you to reverse a list in place. This means that the list is reversed in a memory-efficient manner, without needing to create a new object.
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]
Because the operation is handled in place, if you need to retain the original list, you’ll first need to create a copy of it. We can create a copy of the list by using the .copy()
method on the original list first. 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]
reversed_list = original_list.copy()
reversed_list.reverse()
print('Original List: ', original_list)
print('Reversed List: ', 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]
We can see that by first applying the .copy()
method to the original list, we can then chain in the .reverse()
method to reverse the copied list.
In the next section, you’ll learn how to reverse a list by using list indexing.
Reverse a Python List Using the reversed function
Python has a built-in function, reversed()
, which takes an iterable object such as a list and returns the reversed version of it.
The reversed()
method works similar to the list indexing method covered below. It was added to Python to replicate the indexing method without being overly verbose.
Let’s take a look at how we can use the reverse()
function 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
. Because of this, we need to pass the object into the list()
function to convert it to a list.
We can verify this by writing the following code:
# Checking the Type of a reverseiterator
original_list = [1,2,3,4,5,6,7,8,9]
print(type(reversed(original_list)))
# Returns: <class 'list_reverseiterator'>
By passing the object into the list()
function, all items are pulled into the list.
At its core, the iterator object is quite memory efficient. This is because it behaves like a generator object and evaluates lazily (only when an item is accessed). When we convert it to a list using the list()
function, we lose this efficiency.
In the following section, you’ll learn how to use list indexing to reverse a Python list.
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 intervals. List indexing takes the form of [start:stop:step]
.
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 the 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]
In the code above, we create a new list by stepping over the original list in reverse order. This method doesn’t modify the object in place and needs to be assigned to a variable.
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 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 original_list:
reversed_list = [item] + reversed_list
print(reversed_list)
# Returns: [9, 8, 7, 6, 5, 4, 3, 2, 1]
Let’s break down what the code block above is doing:
- We define two lists, our original list and an empty list to store our reversed items
- We then loop over each item in the list. We reassign the reversed list to a list containing each item plus the reversed list.
In the next section, you’ll learn how to use a Python list comprehension in order to reverse a list.
Reverse a Python List Using a List Comprehension
Similar to using a for loop for this, we can also use a list comprehension to reverse a Python list. 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]
start = len(original_list) - 1
reversed_list = [original_list[i] for i in range(start, -1, -1)]
print(reversed_list)
# Returns: [9, 8, 7, 6, 5, 4, 3, 2, 1]
Let’s break down what the code above is doing:
- We create a variable
start
that represents the length of the list minus one - We then create a list comprehension that accesses each index item in our list using the range from the start position to 0 in reverse order.
This method works, but it’s not the most readable method. It can be a good way to learn how to do this for an interview, but beyond this, it’s not the most recommended method.
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]
reverser = slice(None, None, -1)
reversed_list = original_list[reverser]
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. This allows you to easily apply the same slice to multiple items.
In addition to this, the slicer()
function allows you to give the slice an easy-to-understand name. In the example above, we named the slicer reverser
in order to make it clear what the slice object is doing.
Frequently Asked Questions
The best and most Pythonic way to reverse a list in Python is to use the list.reverse()
method. This method reverses the list in place and is the clearest way to communicate to your reader what you’re doing.
The best way to reverse a list using slicing in Python is to use negative indexing. This allows you to step over a list using -1, by using the code list[::-1]
.
The fastest way to reverse a list in Python is to use a for loop (or a list comprehension). This method is hundreds of times faster than other methods (even when tested against lists with 100,000,000 items).
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 then learned how to use the reverse()
and reversed()
methods to reverse a list. Then, you learned how to use for loops and list comprehensions, as well as the slice function.
Additional Resources
To learn more about related topics, check out the tutorials below:
Your graphic “Indexing Python Lists” on http://datagy.io/python-reverse-list/ has the positive indexing starting at ONE instead of ZERO!
Oof! That’s embarrassing. I have fixed that. Thanks so much for pointing that out.