The reverse() function in python helps to reverse the order of list elements. And hence the list getting updated.
list.reverse()
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.
The reverse() method doesn't return any value. It just updates the list by reversing the order of the elements.
# 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']
# 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']