Python List reverse()

Previous
Next

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.

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.

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.

listOfItems=['Clock','Bed','Fan','Table'] for item in reversed(listOfItems): print(item)

Output:

Table
Fan
Bed
Clock

That’s all about Python List reverse method.

Previous
Next

Add Comment