What is the list reverse() method in Python?
Overview
The list.reverse() method is simply used to reverse the order of the items in a list in Python.
Syntax
list.reverse()
Parmeter value
There are no parameter values.
Example 1
Let’s create a list of even numbers up to ten and then use the list.reverse() method to reverse the order of the items in the list.
Code
even_numbers = [2, 4, 6, 8, 10]# now let us reverse the items in the listeven_numbers.reverse()print(even_numbers)
Explanation
- Line 1: We created a list of even numbers up to ten and we named it
even_numbers. - Line 4: We applied the
list.reverse()method to reverse the items on the list usingeven_numbers.reverse(). - Line 5: We returned the output.
Example 2
Let’s reverse the order of items present in a list of countries.
Code
countries = ['Argentina', 'Niger', 'Italy', 'Russia', 'Australia']# reversing the order of the items in the listcountries.reverse()print(countries)