Python List reverse() Method Tutorial

Python List Reverse() Method

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.

Python List reverse()

As the method name justifies itself, it simply reverses the order of the items or elements of a Python list.

Syntax

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.

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.

Python List Reverse() Method Example

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.

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.

Python List Reverse Using Slicing Operator Example

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.

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.

Python Reversed() Function Example For Python List

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!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *