This is a detailed tutorial of the Python List reverse() method. Learn to reverse the order of items of a Python List using the reverse() method.
Table of Contents
Python List reverse()
As the method name justifies itself, it simply reverses the order of the items or elements of a Python list.
Syntax
1 |
list.reverse() |
reverse()
Parameters
The reverse()
method does not take any arguments because no explicit information is actually required to reverse the order of a list.
reverse()
Return Value
The reverse()
method returns None
because it just performs an action and not a calculation that will return something.
Examples
The following example not just showcase the usage of the method reverse()
but I’ve also included some examples that are helpful as an alternative to the reverse()
method in some practical use-cases.
Example 1. Reversing a list using list.reverse()
method
Here I have got a fruits
list defined containing some fruit names in random order defined by me.
1 2 3 4 5 6 7 8 9 |
#A Python List in some pre-defined order of items fruits = ["Banana","Apple","Mango","Guava","Papaya"] print("fruits List initial order:",fruits) #Reversing the order of the list fruits.reverse() print("fruits List updated order:",fruits) |
The method reverse()
simply reverses the order. In other words, the First item now becomes the last and the last item now becomes the first in the list and similarly, the other items are re-arranged.
Note. The list.reverse()
method reverses the original defined order of the list and not in descending or ascending order. If you want to order the list items in Ascending or Descending order you can use the Python List sort() method.
Example 2. Using Slicing Operator To Reverse A Python List
There’s also another way for reversing a list and that is the use of the Slicing Operator. The following example reverses the order of a Python List using this operator.
1 2 3 4 5 6 7 8 9 10 |
#A Python List in some pre-defined order of items fruits = ["Banana","Apple","Mango","Guava","Papaya"] print("fruits List initial order:",fruits) #Reversing the order of the list #Using Slicing Operator fruits = fruits[::-1] print("fruits List updated order:",fruits) |
The only difference in this example from the first one is that here I’ve used the slicing operator to reverse the list instead of the list.reverse()
method.
Example 3. Loop Through a List in Reverse order using the method reversed()
Let’s say you want to loop through a list in the reverse order. You can first reverse the list using the method list.reverse() and then can use the list variable to iterate through. But what in case, you do not want to alter the original order of the list and just want to reverse loop through just for an instance.
In such a case, it’s better either to use the slicing operator or even there’s a better way to use the Python Built-in Function reversed()
. This Python function reversed()
can reverse the order of items for any Python sequence object.
1 2 3 4 5 6 7 8 9 |
#A Python List in some pre-defined order of items fruits = ["Banana","Apple","Mango","Guava","Papaya"] #Loop through fruits in Reverse order for fruit in reversed(fruits): print(fruit) #Order of Original List Remains the Same print("List order:",fruits) |
Here I’ve simply applied the built-in function reversed()
on the fruits
list and then have used the For Loop to loop through it.
I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.
And do not forget to subscribe to WTMatter!