The Python reverse() method reverses the current order of items in a
list. The first item becomes the last, the second item becomes the second last, and so on.
The method changes the original list in place. It does not create a new list and it does not sort the values.
my_list.reverse()
The reverse() method does not accept any arguments.
my_list=['a', 'b', 'c', 'd', 'x', 'y', 'z']
my_list.reverse()
print(my_list)
Output
['z', 'y', 'x', 'd', 'c', 'b', 'a']
The same elements remain in the list, but their positions are reversed.
The reverse() method works in the same way with numeric lists.
numbers=[1, 2, 3, 4, 5]
numbers.reverse()
print(numbers)
Output
[5, 4, 3, 2, 1]
A list can contain different data types, and reverse() can reverse their positions without comparing the values.
my_list=[1, 'apple', 3.14, True]
my_list.reverse()
print(my_list)
Output
[True, 3.14, 'apple', 1]
Because reverse() only changes positions, the elements do not need to be comparable with each other.
The reverse() method modifies the existing list and returns None.
my_list=['one', 'two', 'three']
result=my_list.reverse()
print(result)
print(my_list)
Output
None
['three', 'two', 'one']
Since reverse() returns None, assigning its return value back to the same variable replaces the list variable with None.
my_list=[1, 2, 3]
my_list=my_list.reverse()
print(my_list)
Output
None
Call reverse() directly when you want to change the existing list.
Reversing a list and sorting a list in descending order are different operations.
The reverse() method only flips the current sequence.
my_list=[4, 2, 9, 1]
my_list.reverse()
print(my_list)
Output
[1, 9, 2, 4]
To arrange values from highest to lowest, use the
sort() method with reverse=True.
my_list=[4, 2, 9, 1]
my_list.sort(reverse=True)
print(my_list)
Output
[9, 4, 2, 1]
Therefore, reverse() should not be used when the actual requirement is to sort values in descending order.
We can also reverse a list by using
list slicing with a step value of -1.
my_list=[1, 2, 3, 4]
new_list=my_list[::-1]
print(new_list)
print(my_list)
Output
[4, 3, 2, 1]
[1, 2, 3, 4]
The difference is important:
reverse() changes the original list.my_list[::-1] creates a new reversed list and leaves the original list unchanged.
Python also provides the
reversed() function. It returns an iterator that reads the items in reverse order without modifying the original list.
my_list=['Alex', 'Ronald', 'John']
new_list=list(reversed(my_list))
print(new_list)
print(my_list)
Output
['John', 'Ronald', 'Alex']
['Alex', 'Ronald', 'John']
Use reverse() when the existing list should be changed. Use reversed() when you want to process the items in reverse order without changing the original list.
If a list contains other lists, reverse() reverses only the order of the outer list. It does not automatically reverse the items inside each inner list.
my_list=[
[1, 2],
[3, 4],
[5, 6]
]
my_list.reverse()
print(my_list)
Output
[[5, 6], [3, 4], [1, 2]]
The inner lists keep their own element order. Read more about 2D lists in Python.
reverse() reverses the current order of list items.None.my_list[::-1] when a new reversed list is required.reversed() to iterate through items in reverse order without changing the original list.sort(reverse=True) when values need to be sorted in descending order.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.