How Can We Help?
In this tutorial, we are going to explain how to reverse Python list Method with basic syntax and many examples for better understanding.
How to reverse a list in Python? To reverse a list in Python, We use the reverse() method. The reverse method with the list reverses the elements of the list in place and returns the updated reversed list. ex: abcd reversed as dcba.
Syntax:
<list_variable_name>.reverse()
Input Parameters:
- list_variable_name : The lists that we want to reverse it.
Return Value:
Python List reverse() method doesn’t return any value (returns None). It updates the current list in place.
Reverse List using reverse() Method Example
# Alphabet List
alphabet=['a', 'b', 'c', 'd']
# Reversing alphabet list
alphabet.reverse()
# Print updated alphabet list
print("Updated alphabet list: ",alphabet)
Output:
Updated list: [‘d’, ‘c’, ‘b’, ‘a’]
In the above example, the list variable ‘alphabet’ is reversed by ” alphabet. reverse()” and returns an updated list with reverse values.
Reverse List using Slicing Operator Example
# Color List
colorList = ['Yellow', 'Red', 'Black']
print('Original List:', colorList)
# Reversing color list
# Syntax: reversed_color_list = colorList[start:stop:step]
reversed_color_list = colorList[::-1]
# Print updated color list
print('Updated color list:', reversed_color_list)
Output:
Updated color list: [‘Black’, ‘Red’, ‘Yellow’]
In the above example, using the slicing feature available with Python reverse list. we can create a new reversed copy of our list.
Iterate Through List in Reverse Using Reversed() Method Example
# Color List
colorList = ['White', 'Red', 'Black']
# Printing Elements in Reversed Order
for elements in reversed(colorList):
print(elements)
Example Output:
Black
Red
White
In the above example, using reversed() method with for loop we have iterated over the elements of a list in reverse order. By using reversed() method it does not update the original list.
Create a Reversed Copy of a List Using List(reversed()) Function Example
# Alphabet List
alphabetList = ['a', 'b', 'c', 'd']
# Create new list of reversed alphabet list
reversedalphabetList = list(reversed(alphabetList))
# Print updated alphabet list
print(reversedalphabetList)
In the above example, we have created a new reversed copy of our alphabet list called reversedalphabetList.
In this tutorial, you have learned how to reverse a list using the reverse() method. we have demonstrated by giving different examples like normal reverse() method, using slicing operator, Iterate through a list in reverse using reversed() built-in function, and how to create a new reversed copy of a list.
Hopefully, it was clear and concise.
If you have an inquiry or doubt don’t hesitate to leave them in the comment section. we are waiting for your feedback.
Similar Python list Methods:
- list.append(object) : It appends object to the given list.
- list.count(object) : It returns count of how many times object occurs in the list.
- list.extend(seq) : It appends object to the given list.
- list.insert(index,object) : It inserts object into the list at offset index.
- list.pop(obj=list[-1]) : It removes last object from the list and returns removed object of a list.
- list.remove(object) : It removes object from the list.
- list.sort([func]) : It sorts objects of a list and use compare func, if given.
Related Articles
Python Lists – Mastering By Practical Examples (Comprehensive Guide)
Python tutorials list:
- Python tutorials list : accessing all python tutorials.