Javatpoint Logo
Javatpoint Logo

Python List reverse() Method

Python reverse() method reverses elements of the list. If the list is empty, it simply returns an empty list. After reversing the last index value of the list will be present at 0 index. The examples and method signature is given below.

Signature

Parameters

No parameter

Return

It returns None.

Let's see some examples of reverse() method to understand it's functionality.

Python List reverse() Method Example 1

Let's first see a simple example to reverse the list. It prints all the elements in reverse order.

Code

Output:

['e', 'l', 'p', 'p', 'a']

Explanation:

In the above example, we've utilized the reverse() method to reverse the elements in the list apple. We've called the reverse() method on apple and afterwards printed the list to see the reversed elements. The result is [' e ', ' l ', ' p ', ' p ', ' a '], which shows that the reverse() method has effectively reversed the order of the elements in the list.

Python List reverse() Method Example 2

Utilizing slicing to reverse a list. In addition to the reverse() method, we can likewise involve slicing to reverse the elements in a list. Here is an example:

Code

Output:

[5, 4, 3, 2, 1]

Explanation:

In the above code, we've involved slicing to reverse the elements in the my_list. We've made another list named reversed_list by utilizing the slicing procedure with a stage value of - 1, which reverses the order of the elements. We've then printed the reversed_list to see the reversed elements. The result is [5, 4, 3, 2, 1], which shows that the slicing method has effectively reversed the order of the elements in the list.

Python List reverse() Method Example 3

Reversing a list of tuples. The reverse() method can likewise be utilized to reverse the order of elements in a list of tuples. Here is an example:

Code

Output:

[( 4, ' date '), ( 3, ' cherry '), ( 2, ' banana '), ( 1, ' apple ')]

Explanation:

In the above code, we've made a list of tuples named my_list and afterward utilized the reverse() method to reverse the order of the tuples. We've then printed the my_list to see the reversed tuples. The result is [(4, 'date'), (3, 'cherry'), (2, 'banana'), (1, 'apple')], which shows that the reverse() method has effectively reversed the order of the tuples in the list.

Python List reverse() Method Example 4

It returns empty list if the list is the list is empty. See the example below.

Code

Output:

[ ]

Python List reverse() Method Example 5

This example demonstrate that after reversing order of element does not change.

Code

Output:

Both are equal

Conclusion:

The reverse() method is an implicit function in Python that reverses the order of the elements in a list. It's a valuable method for reordering lists of elements. In this article, we've investigated the reverse() method and how to utilize it. We've likewise shown examples of utilizing the reverse() method to reverse a list of numbers, a list of strings, and a list of tuples. We trust this article has been useful in showing how to utilize the reverse() method in Python.


Next TopicPython Lists





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA