The reverse()
function is a method available for lists in Python. It reverses the elements of the list in place, meaning it modifies the original list directly by reversing the order of its elements.
Parameter Values
This function does not accept any parameters.Return Values
The reverse()
method does not return a value; it modifies the list in place.
How to Use reverse()
in Python
Example 1:
The reverse()
method reverses the elements of a list in place.
numbers = [1, 2, 3, 4, 5]
numbers.reverse()
print(numbers)
Example 2:
The reverse()
method works for any type of elements in the list, not just integers.
fruits = ['apple', 'banana', 'orange', 'kiwi']
fruits.reverse()
print(fruits)
Example 3:
The reverse()
method does not return a new reversed list but modifies the existing list.
colors = ['red', 'green', 'blue', 'yellow']
colors.reverse()
print(colors)