Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.
Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.
For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.
It makes sense to start learning by reading and watching videos about fundamentals and how things work.
Data Science and Machine Learning - 16 wks
Full-Stack Software Developer - 16w
Search from all Lessons
Curated list of small interactive and incremental exercises you can take to get better at any coding skill.
Curated section of projects to build while learning with simple instructions, videos, solutions, and more.
Guides on different topics related to the technologies that we teach in our courses
Here is a simple way to reverse a list in Python using the
Reversing a list is a very common operation in the day of a programmer, you will find cases in which you will need a list similar to the original one with the same elements, but in a different order, in this case, we could say that we would be "flipping" the list of interest.
For these cases, Python has various methods and ways to "flip" or "reverse" a list. Let's see more information on these methods/ways in the following sections.
Our first option to reverse a list in Python is the
.reverse() built-in method. Every list object in Python has a method called reversed that you can call on any list object and it will reverse the list in place. This method does not create a new list, instead, the method modifies the original list. We can see another example of this method as follows:
As mentioned before, this method does not returns any values nor create a new list, it modifies our original list in place, and, as you can see, this method does not need any parameters, to reverse any list we just need to put the list name alongside the
.reverse() method like this
The second option to reverse a list in Python is the
reversed() built-in function. This option won't modify the original list nor will it create a copy of it, instead, it will return an iterator which we can use to loop through all the elements of the original list in order to move through the elements of the list in reverse. Basically, the
reversed() built-in function will return a reverse iterator object. Let's see an example below:
See that, if we only use the
reversed() function, we will only get a reverse iterator object, so we need to convert this iterator into the reverse list that we want. To do this we need to use a second function called
list(), what this function will do is go through each and every one of the elements of our original list and store these elements in a new list. Let's see how to use both
reversed() functions as follows:
The third option is to use the list slicing syntax. Python list objects have a feature called list slicing, unlike the first method (
.reverse()), this option does not modify our original list, rather it creates a copy of it and returns this copy with the elements of the original list flipped or reversed.
To use this list slicing syntax, we just need to write our list name alongside some square brackets with a double column and a -1 inside these brackets like this
[::-1], with our list name it would result as follows
listname[::-1]. Let's see an example of this method below:
As you can see, if we print our original list it won't be flipped as this method does not modify the original list. You can assign a variable to the copy of the original list as follows:
Basically, what the list slicing syntax is doing is giving us a slice of a list, we can specify the slice that we want to get off the list by specifying the start, end, and step parameters into the square brackets like this
mylist[start:end:step]. So we can see this syntax as a "trick" to flip any list. Let's see some more examples below so we can better understand how this list slicing syntax works:
See that putting the 2 columns
:: without any start or end gives us the whole list, and if we specify a step of
-1 this would return the whole list but flipped. This is how this "trick" works.
To summarize a little bit of what has been explained in this article, we were able to see 3 ways to reverse lists in Python. You may be wondering what the best option is or in what situations it is better to use a certain option to reverse lists in Python, and we hope that the following summary will help you define your preferred way or method:
.reverse() Method: this option will reverse a list in place, to use it you just need to type your list name alongside the
.reverse() word like this
list.reverse(). This method modifies the original list and won't create a copy of it.
.reversed() Function: this option won't give us a copy nor will modify the original list, this function will give us a reverse iterator that needs to be converted into a list using the
list() function. So the
.reversed() function needs to be used alongside the
list() function as we saw in the example above to get our desired reversed list.
List Slicing Syntax: this option will return a reversed copy of our original list instead of modifying it. To use this "trick" you just need to write the list name alongside some square brackets with a double column and a -1 inside these brackets like this
[::-1], with our list name it would result as follows
Hope this article would help you define how to reverse a list in Python, keep practicing!