Python reverse list; Through this tutorial, i am going to show you how to reverse a list in python using for loop , slicing and reverse function.
Reverse list python
See the following python programs for how to reverse the list: as shown below:
- 1: Python reverse list using the reverse function
- 2: How to reverse a list in python using for loop
- 3: How to reverse a list in python using slicing
1: Python reverse list using the reverse function
The python reverse() function is used to reverse the elements/items of a given list in python.
The syntax of reverse() method is:
list.reverse()
Note:- The reverse() function doesn’t take any argument. And The reverse() function doesn’t return any value. It only reverses the elements and updates the list.
Python program to reverse a list using reverse() function is below:
# Laptop brand list lp = ['Dell', 'HP', 'Acer'] print('Original List:', lp) # List Reverse lp.reverse() # updated list print('Updated List:', lp)
Output
Original List: ['Dell', 'HP', 'Acer'] Updated List: ['Acer', 'HP', 'Dell']
2: How to reverse a list in python using for loop
python program to reverse a list using for loop Or python program to reverse a list without using reverse function:
# Laptop brand list lp = ['Dell', 'HP', 'Acer'] print('Original List:', lp) # Print Python list element in reverse order using for loop for l in reversed(lp): print(l)
Output
Original List: ['Dell', 'HP', 'Acer'] Acer HP Dell
3: How to reverse a list in python using slicing
python program to reverse a list using slicing and wihout using any built-in python list function/method:
def Reverse(lst): new_lst = lst[::-1] return new_lst # Laptop brand list lp = ['Dell', 'HP', 'Acer'] print('Original List:', lp) # List Reverse print('Updated List:', Reverse(lp))
Output
Original List: ['Dell', 'HP', 'Acer'] Updated List: ['Acer', 'HP', 'Dell']