Python List reverse()

Python list‘s reverse method is used to reverse the list.It does not return any value but updates the original list.


Syntax

Here list1 is object of the list.reverse() method does not take any parameter.


Return

does not return anything just updates the list.


Example

Let’s see a very simple example:

listOfAnimals=['Dog','Cat','Rat','Elephant','Cow'] print('Original list:',listOfAnimals) listOfAnimals.reverse() print("Reversed list:",listOfAnimals)

Output:

Original list: [‘Dog’, ‘Cat’, ‘Rat’, ‘Elephant’, ‘Cow’]
Reversed list: [‘Cow’, ‘Elephant’, ‘Rat’, ‘Cat’, ‘Dog’]

Let’s see how to iterate over list in reversed order.You can use Python’s reversed function to do it.
Let’s understand with the help of example.

listOfAnimals=['Dog','Cat','Rat','Elephant','Cow'] for animal in reversed(listOfAnimals): print(animal)

Output:

Cow
Elephant
Rat
Cat
Dog

Leave a Reply

Your email address will not be published. Required fields are marked *