Python program for Reversing a List
In this tutorial, we will discuss the different approaches for reversing a list in Python. For a given list, we have to reverse the order of all the elements in the list and print the reversed list.
In Python, there are three ways for reversing a list, we will be discussing all the ways in detail.
For a given list, our program will return the list after reversing:
Input: [ 'Study', 'tonight', 1]
Output: [1, 'tonight', 'study']
Input: [14, 5, 2, 8, 19]
Output: [19, 8, 2, 5, 14]
Approach to Reverse the list in Python
In Python, there are various ways of reversing a list. We will discuss three of them in this tutorial.
- Using reverse() method
- Accessing in reverse order
- Reversing using slicing
Let us look at these approaches in detail separately
Approach 1: reverse() method
In this approach, we will use a built-in method reverse() which will reverse a given list. This method will modify the existing list and copy the elements in reverse order.
Algorithm
Step 1- Initialise list
Step 2- Use list.reverse()
Step 3- Print reversed list
Python Program 1
Look at the program to understand the implementation of the above-mentioned approach.
#reversing a list
#using reverse()
list = ['Study','tonight', 1, 2, 3]
print("Existing list",list)
# reversing
list.reverse()
print("After reversing all elements")
print(list)
Existing list ['Study', 'tonight', 1, 2, 3]
After reversing all elements
[3, 2, 1, 'tonight', 'Study']
Approach 2: accessing in reverse order
In this approach, we will access the elements of a list in reverse order and then print the list. To access all the elements in reverse, we will use reversed() function which returns the iterator to access each element in reverse order.
Algorithm
Step 1- Initialise list
Step 2- Run a loop for each element
Step 3- Use reversed() to get iteration of elements in reverse order
Step 4- Print elements
Python Program 2
Look at the program to understand the implementation of the above-mentioned approach.
#reversing a list
list = ['Study','tonight',11, 5, 6]
print("Existing list",list)
# printing in reverse
print("After reverse all elements")
#using reversed()
for i in reversed(list):
print(i)
Existing list ['Study', 'tonight', 11, 5, 6]
After reversing all elements
6
5
11
tonight
Study
Approach 3: slicing in Python
In this approach, we will declare a new list and then use the slicing operator (:) to copy the elements in reverse order in the new list.
Algorithm
Step 1- Initialise a function to reverse list
Step 2- Declare new list
Step 3- Store elements in lew list in reverse order
Step 4- Print reversed list
Python Program 3
Look at the program to understand the implementation of the above-mentioned approach.
#reversing a list
#function to reverse list
def rev(list):
# creating new list
new_list = list[::-1]
return new_list
list = [2, 45, 21, 15, 6]
print("Existing list",list)
print("After reversing all elements")
print(rev(list))
Existing list [2, 45, 21, 15, 6]
After reversing all elements
[6, 15, 21, 45, 2]
Conclusion
In this tutorial, we have learned three ways of reversing a list in Python. We have also learned how to use built-in functions like reverse() and reversed() to get the desired list. We have also used the concept of slicing.