Tutorial Study Image

Python reverse()

The reverse() function in python helps to reverse the order of list elements. And hence the list getting updated.


list.reverse() 
 

reverse() Parameters:

The reverse() method doesn't take any parameters. If we need individual elements of a list in the reverse order, we can use reversed() function.

reverse() Return Value

The reverse() method doesn't return any value. It just updates the list by reversing the order of the elements.

Examples of reverse() method in Python

Example 1: How to reverse a List in Python?


# alphabets list
alphabets = ['a', 'b', 'c', 'd', 'e', 'f']
print('Original List:', alphabets)

# List Reverse
alphabets.reverse()

# updated list
print('Updated List:', alphabets)
 

Output:


Original List: ['a', 'b', 'c', 'd', 'e', 'f']
Updated List: ['f', 'e', 'd', 'c', 'b', 'a']

Example 2: How to reverse a List using slicing operator


# alphabets list
alphabets = ['a', 'b', 'c', 'd', 'e', 'f']
print('Original List:', alphabets)

# Reversing a list 
#Syntax: reverselist = alphabets[start:stop:step] 
reverselist = alphabets[::-1]

# updated list
print('Updated List:', reverselist)
 

Output:


Original List: ['a', 'b', 'c', 'd', 'e', 'f']
Updated List: ['f', 'e', 'd', 'c', 'b', 'a']