Python List reverse() Method

The reverse() method reverses the position of elements in the list. The first element becomes the last element, the second element becomes the second last element, and so on.

Syntax:

list.reverse()

Parameters:

No parameters.

Return type:

No return value.

The following example demonstrate the list.reverse() method.

Example:
cities = ['Mumbai', 'London', 'Chicago', 'Paris', 'New York']
print("Original List: ", cities)
cities.reverse()
print("Reversed List: ", cities)
Output
Original List:  ['Mumbai', 'London', 'Chicago', 'Paris', 'New York']
Reversed List: ['New York', 'Paris', 'Chicago', 'London', 'Mumbai']

Above, the first element 'Mumbai' repositioned as the last element, and the last element 'New York' repositioned as the first element. In the same way, the second element becomes the second last element. However, the middle element 'Chicago' remains in the same position.

The following reverses the integer list.

Example: reverse()
nums = [1, 2, 3, 4, 5]
print("Original List: ", nums)

nums.reverse()
print("Reversed List: ", nums)
Output
Original List: [1, 2, 3, 4, 5]
Reversed List: [5, 4, 3, 2, 1]

You can use the slicing operator [::-1] to get a new reversed list, as shown below.

Example: reverse()
nums = [1, 2, 3, 4, 5]
rev_nums = nums[::-1]

print("Original List: ", nums)
print("Reversed List: ", rev_nums)
Output
Original List: [1, 2, 3, 4, 5]
Reversed List: [5, 4, 3, 2, 1]
Want to check how much you know Python?