Reverses the order of the list
Usage
The reverse()
method reverses the order of list. This method does not return anything; it modifies the list in place.
Syntax
list.reverse()
Examples
L = ['red', 'green', 'blue']
L.reverse()
print(L)
# Prints ['blue', 'green', 'red']
L = [1, 2, 3, 4, 5]
L.reverse()
print(L)
# Prints [5, 4, 3, 2, 1]
Access List Items in Reversed Order
If you don’t want to modify the list but access items in reverse order, you can use reversed() built-in function. It returns the reversed iterator object, with which you can loop through the list in reverse order.
L = ['red', 'green', 'blue']
for x in reversed(L):
print(x)
# blue
# green
# red