Python List reverse()
Python List reverse() method
Python List reverse() method reverses the order of items in the list in place.
In this tutorial, you will learn the syntax of, and how to use List reverse() method, with examples.
Syntax of List reverse()
The syntax of List reverse() method is
list.reverse()
Parameters
reverse() method take no parameters.
Return value
reverse() method returns None.
Please note that reverse() method modifies the original list.
Examples
1. Reverse the given list in Python
In the following program, we take a list my_list with values [2, 4, 6, 8, 10]
. We have to reverse this list.
Call reverse() method on the list my_list.
Python Program
my_list = [2, 4, 6, 8, 10]
my_list.reverse()
print(my_list)
Output
[10, 8, 6, 4, 2]
The order of items in the list is reversed.
2. Reverse the given list of strings in Python
In the following program, we take a list my_list with some initial string values.We have to reverse this list.
Call reverse() method on the list my_list.
Python Program
my_list = ['apple', 'banana', 'cherry', 'mango']
print(f"Original list : {my_list}")
my_list.reverse()
print(f"Reversed list : {my_list}")
Output
Original list : ['apple', 'banana', 'cherry', 'mango']
Reversed list : ['mango', 'cherry', 'banana', 'apple']
The list is reversed.
Summary
In this tutorial of Python Examples, we learned about List reverse() method, how to use reverse() method to reverse the list in place, with syntax and examples.