In this tutorial, We are going to learn the Python list reverse() method to reverse the list items. list reverse function is a built-in function in Python.
Many times, you want to reverse the elements from the list, Then you can use the list reverse() built-in function. In the previous tutorial, we have seen the Python list insert() method.
Headings of Contents
Python List reverse() Method:
In Python, the reverse() method is the list method that is used to reverse the items of the list.
reverse() method Syntax:
Syntax of list reverse() method is:
list.reverse()
reverse() method parameter:
List reverse function does not take any parameter.
reverse() method return type.
List reverse() method does not have any value. it updates the existing list.
List reverse() method Example:
Here we will see two different ways to reverse the items of the list, the first is using the list reverse() method and the second is using slice syntax.
Example
lang = ['Python', 'Java', 'C', 'C++', 'C++', 'JavaScript', 'HTML']
print('Original List:- ', lang)
lang.reverse()
print('Updated List:- ', lang)
Output
Original List:- ['Python', 'Java', 'C', 'C++', 'C++', 'JavaScript', 'HTML']
Updated List:- ['HTML', 'JavaScript', 'C++', 'C++', 'C', 'Java', 'Python']
In the above example, we have a list named lang, we have reversed the list lang using the list reverse function.
There is another way to reverse a list using slice syntax:
Example
lang = ['Python', 'Java', 'C', 'C++', 'C++', 'JavaScript', 'HTML']
print('Original List:- ', lang)
reversed_list = lang[::-1]
print('Updated List:- ', reversed_list)
Output
Original List:- ['Python', 'Java', 'C', 'C++', 'C++', 'JavaScript', 'HTML']
Updated List:- ['HTML', 'JavaScript', 'C++', 'C++', 'C', 'Java', 'Python']
Conclusion:
In this tutorial, you have learned how to use the list reveres() method to reverse the list items. list reverse method is a very useful method to reverse the items of the list.
Hope this tutorial will help you. If you like this article, please share it with your friends who want to learn
python programming.
For More Information:- Click Here