Python List reverse() method
Python List reverse() method
The list.reverse () method in Python reverses the elements of the list in place.
Syntax
list.reverse()
Parameter
NA
Example 1
# Python program explaining # the list.reverse() method weekList = ['Sun','Thurs','Mon','Fri', 'Tues', 'Wed'] print("Actual week list: ",weekList) # Reversing the week list by calling the reverse() method weekList.reverse() # printing the reverse week list print("Reversed week list:",weekList)
Output
Actual week list: ['Sun', 'Thurs', 'Mon', 'Fri', 'Tues', 'Wed'] Reversed week list: ['Wed', 'Tues', 'Fri', 'Mon', 'Thurs', 'Sun']
Example 2
# Python program explaining # the list.reverse () method # passing the number list num = [11, 13, 14, 12,15] print("Actual List: ",num) # Sorting list of Integers in descending num.sort(reverse = True) # printing the reverse order print("Reverse List: ",num)
Output
Actual List: [11, 13, 14, 12, 15] Reverse List: [15, 14, 13, 12, 11]
Example 3
# Python program explaining # the list.reverse() method # passing the number List numList = [1,2,3,4,5,6,7,8,9,10,12] print("Actual List: ",numList) # Reversing the Order of the elements # Syntax: reversed_list = os[start:stop:step] reversed_list = numList[::-1] # printing the list print('Updated List:', reversed_list)
Output
Actual List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12] Updated List: [12, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]