How to Reverse a List in Python?

Reversing a list is a common operation in Python. It is often used in conjunction with other list operations such as slicing, concatenation, and sort. There are several ways to reverse a list in Python. In this article we will explore six different methods to accomplish this.

Using a For Loop to reverse a Python List

One way to reverse a list is to use a for loop. The for loop will iterate over the elements of the list from the last element to the first element and add each element to a new list. This method mutates the original list which means that if you need to preserve the original list you will need to make a copy of it before using this method. An example of this can be seen below:

my_list = [1,2,3,4,5,9] reversed_list = [] for elem in my_list[::-1]: reversed_list.append(elem) print(reversed_list) #prints out [9,5,4,3,2,1]
Code language: PHP (php)

Using List Indexing to reverse a Python List:

Another way that you can reverse a list is by using list indexing. You can use negative indexes starting from the end of the list to select elements from the end of the list and add them to a new list.

This method also mutates the original list so if you need to keep the original intact you should make a copy first before using this method as well. Below is an example of how this would work:

my_list = [1,2,3,4] reversed_list = [] for elem in my_list: reversed_list = [elem] + reversed_list #add each element to the beginning of our new list print(reversed_list) #prints out [4,3,2,1] which is our new reversed lists within lists!
Code language: Python (python)

Using .reverse() method:

If you don’t mind mutation and your program doesn’t require you to make copies then you can use the .reverse() method that all lists have as part of their functionality.

This method reverses the elements of your target lists IN PLACE meaning that it won’t return anything, rather it just mutated your target object directly.

See how it works below on our original my_list from above:

my_list = [1,2,3,4] my_list.reverse() #the .reverse() method doesn't return anything but it does reverse our target object in place! print(my_list) #now when we go to print my_list we see that it outputs [4,3,2,1] instead! print(my_list[0])
Code language: Python (python)

Using a List Comprehension to reverse a Python List

We can also use a list comprehension to do this, which is a lot like using a for a loop. We won’t just change the for loop into a list comprehension, though. Instead, we’ll take a slightly different approach.

We’ll go over each item on our original list again and again, but this time in reverse order. Let’s see what this looks like:

# Reverse a Python list with a List Comprehension my_list = [1, 2, 3, 4] start = len(my_list) - 1 reversed_list = [my_list[i] for i in range(start, -1, -1)] print(reversed_list) # Output: [4, 3, 2, 1]
Code language: PHP (php)

Let’s look at what the above code does:

We make a variable called “start” that is equal to the list’s length minus one.

Then, we make a list comprehension that uses the range from start to 0 in reverse order to get to each index item in our list.

This way works, but it’s not the easiest to understand. It can be a good way to learn how to do this for an interview, but it’s not the best way to learn how to do it in general.

Reverse a Python List Using the Slice Method

Python also has a function called slice() that lets us index an object in the same way that we did above with a list. The function makes a slice object, which makes it easy to use the same slice in more than one object. We could use the same indexing on different lists because of this.

Let’s look at how the function can be used in Python:

# Reverse a Python list with slice() my_list = [1,2,3,4,5] reverser = slice(None, None, -1) reversed_list = my_list[reverser] print(reversed_list) # Returns: [5, 4, 3, 2, 1]
Code language: PHP (php)

With this method, we can easily assign a custom slice that can be used again and again without having to change many things. This makes it easy to use the same slice on more than one item.

Also, the slicer() function lets you give the slice a name that is easy to understand. In the above example, we gave the slice object the name reverser to make it clear what it does.

Using reversed() function

Let’s take a look at how we can use the reverse() function to reverse a Python list:

# Reverse a Python list with reversed() my_list = [1,2,3,4,5,6] reversed_list = list(reversed(my_list)) print(reversed_list) # Returns: [6, 5, 4, 3, 2, 1]
Code language: PHP (php)

More about reversed() function

Python’s built-in reversed() function, part of the standard library, returns an iterator that yields items of the iterable in reverse order.

The original iterable is left untouched. Per the docs, “reversed() is equivalent to having all the elements in place and then doing __list.reverse()__.” Considering this behavior and how expensive (or impossible) it might be to actually reverse some data structures “in place,” it is worth considering when reversed() might be a good option for your code.

Given that reversed() yields items one at a time as needed rather than returning a whole list at once, it can save memory in cases where the resulting list would otherwise be too large to fit in memory.

Reversed strings, tuples, and lists all return iterator objects. Iterators are returned by functions and constructs such as range(), map(), filter(), compress(), and zip().

They allow you to make your own objects that you can treat like lists but without actually storing all the values in memory all at once.

A for loop can iterate over any iterable object— not just lists! So even though you are not explicitly using a list here, the for loop will happily go through each item returned from reversed().

You can see how we can take advantage of this behavior to save memory by first reversing only part of a very large list:

for i in reversed(range(100000)): # do something with i print(i)
Code language: PHP (php)

This code snippet uses range() to generate a list of numbers from 0 up to 100,000 and then step through them in reverse order using reversed().

Note that when using range() like this with a single parameter N, it returns a list-like object containing all numbers from 0 up to N-1—hence why we had to use 100000 rather than 100001 as our input value.

If we had written 100001 instead, our output would have included 100000 rather than stopping at 99999. Of course, if we had wanted to include 100000 as our output value, we could have done so by writing range(100001).

If we want our for loop to iterate over only part of this very large list generated by range(), we need another way besides reversed() or slicing to specify which part of the list we want our loop to access. We’ll talk about this more later on.

For now, bear in mind that if you want your for loop only iterate over some (rather than all) of an iterable—even one that would normally return a list if you called list() on it—you can use range()

for i in reversed(range(100000)[::1000]): # do something with i print(i)
Code language: PHP (php)

In this variant on our previous code snippet, notice how we passed [::1000] into range(). This strange looking bit of Python indexing syntax returns every thousandth element from our very large inputted list—i.e., 0, 1000, 2000,…999000—which is perfect for what we want here! We could also have used any other step size besides 1000 (e.g., 500 or 10).

Finally note how if you don’t pass anything into either side of the colon as shown above (i.e., no number before or after the colon), Python defaults to including every element from start to finish just like omitting the slice notation altogether would:

for i in reversed(range(100000)[0:]): # do something with i print(i) # prints same thing as above!
Code language: PHP (php)

If you have an extremely large inputted list but know you only need information from some small section near its end (or beginning), consider using reversed() with slice notation inside of a for loop as shown above rather than creating an entirely new copy of your inputted iterable just so you can access part of it via normal indexing syntax! Doing so will help keep your program’s memory usage low and therefore running smoothly.

As always when coding, think about what exactly your code is doing under-the-hood and whether there might be more efficient ways for it accomplishes its goal!

So there are many different ways that you can reverse lists in python depending on your needs such as using loops or comprehensions or even built-in functions or methods! No matter what though all these six methods shown will get the job done! Thanks for reading and hope learned something today! Happy Coding! 🙂

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts