In this tutorial, we will see about Python List‘s reverse method.Python List reverse method is used to reverse the list.
Python List reverse example
You can simply call reverse method to reverse the list.
Let’s understand this with the help of simple example.
1 2 3 4 5 6 |
listOfItems=['Clock','Bed','Fan','Table'] print("listOfItems:",listOfItems) listOfItems.reverse() print("listOfItems in reversed order:",listOfItems) |
Output:
listOfItems: [‘Clock’, ‘Bed’, ‘Fan’, ‘Table’]
listOfItems in reversed order: [‘Table’, ‘Fan’, ‘Bed’, ‘Clock’]
As you can see here, list got reversed using reverse method.
There are other ways to reverse the list. You can use slicing to reverse a list.
1 2 3 4 5 6 |
listOfItems=['Clock','Bed','Fan','Table'] print("listOfItems:",listOfItems) listOfItems=listOfItems[::-1] print("listOfItems in reversed order:",listOfItems) |
Output:
listOfItems: [‘Clock’, ‘Bed’, ‘Fan’, ‘Table’]
listOfItems in reversed order: [‘Table’, ‘Fan’, ‘Bed’, ‘Clock’]
If you just want to traverse in reverse order, you can use reversed function too.
1 2 3 4 5 |
listOfItems=['Clock','Bed','Fan','Table'] for item in reversed(listOfItems): print(item) |
Output:
Table
Fan
Bed
Clock
Fan
Bed
Clock
That’s all about Python List reverse method.
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.