Python List - Reverse

Method Name:

reverse

Method Signature:

reverse()

Method Overview:

  • This method reverses the ordering of the elements in a Python list object.

Parameters and return value:

Reversing of operation happens in-place and the method does not return any value.

Example:

# Example Python program that reverses the order of elements
# present in a Python list object 
cityList = ["Dublin", "London", "Paris", "Brussels", "Amsterdam", "Berlin", "Warsaw"]
print("List of cities:");

print(cityList)
cityList.reverse()

print("List of cities in reverse order:");
print(cityList)

Output:

List of cities:

['Dublin', 'London', 'Paris', 'Brussels', 'Amsterdam', 'Berlin', 'Warsaw']

List of cities in reverse order:

['Warsaw', 'Berlin', 'Amsterdam', 'Brussels', 'Paris', 'London', 'Dublin']

 


Copyright 2023 © pythontic.com