In this Python guide, we’ll learn how to use Python list reverse method. We’ll be going through multiple examples to implement and practically understand the purpose of reverse method in Python list.
After reading this guide, you’ll be able to easily use Python list reverse method in your own code.
So let’s get right into its implementation.
What is Python List Reverse?
As the name suggests, this method is used to reverse the order of Python list items. We’ll now practically understand how this reverse method works by using easy but proper examples.
Let’s first understand its syntax so we can easily use it in examples as well.
Syntax of Reverse Method
As you can see here, the reverse method doesn’t accept any arguments. Just use it on the list you want to reverse the order of.
Implementing Python List Reverse Method (Multiple Examples)
See below examples to better understand how reverse method works in Python lists.
Example 1: Reverse a Python List
listofItems= [1,2,3,4,5] listofItems.reverse() // reversing the list print(listofItems) // [5, 4, 3, 2, 1]
We have first created a list of integers. Then we have applied the reverse method on it and as a result, the list is successfully reversed.
Example 2: Reverse a Nested List
listofItems= [[1,2],[3,4],[5,6]] listofItems.reverse() print(listofItems) // [[5, 6], [3, 4], [1, 2]]
This is how we can reverse the nested lists as well. You can use the same process to reverse the order of tuples and sets inside a list.
Example 3: Use Slicing Operator to Reverse a List
You can also use this method to reverse the order of Python list. Benefit of using the slicing method is that you can specify the steps. It means whether you want to reverse all the items of list or just want some specific items. See below example:
listofItems= [1,2,3,4,5,6,7,8,9,10] print(listofItems[::-2]) // [10, 8, 6, 4, 2]
This is how it looks [start: end: step which in our case is -2]. Giving it negative value will start the list from end/right. If we use -1 then it will take all the items of list in reverse order. If we specify -3 then it will skip two items and use the third one and so on.
Example 4: Access Items Of List in Reverse Order
listofItems= [1,2,3,4,5,6,7,8,9,10] for item in reversed(listofItems): print(item) // 10 9 8 7 6 5 4 3 2 1
You can make use of the reversed function if you want to reverse each item of list separately. Benefits of using this method is that you can use an if condition on some items if you don’t want them in your list and can use some other actions as well. See below code:
listofItems= [1,2,3,4,5,6,7,8,9,10] for item in reversed(listofItems): if(item!=3): print(item) // 10 9 8 7 6 5 4 2 1
You can see that 3 is not printed as specified using the condition.
Example 5: Reverse Method returns None
listofItems= [1,2,3,4,5,6,7,8,9,10] newVar=listofItems.reverse() print(newVar) // none
You can see that none is returned by the reverse method. It just updates the list and not storing its data.
So this is how you can properly use Python list reverse method. We have also discussed slicing and reversed function to give you more choices to choose from.
Feel free to ask if you still have any questions regarding the implementation of Python list reverse method. I’d be more than happy to answer all your questions.
To conclude this post, hope you now have a detailed practical knowledge of how to use Python list reverse method. Your feedback would be much appreciated. Thank you for reading this post.