Python List is one of the flexible and widely used data type. Some properties of the python list are –
- It is mutable
- It is ordered
- Multiple data types can be stored in the same list.
Since the list is so flexible, reversing a list in python is very simple and easy to understand if you know the basic syntax and some basic operations like loops and array operations.
We can reverse the list in two manners-
- By directly modifying the original list
- By creating a new list and adding elements in reverse order
Also, there are some ways in which we can reverse a list in python which we will be learning in this article, some of the ways are-
- reverse() method
- reversed() method
- slicing method
- Two pointer approach
Before moving to each way let us understand what python reverse list is with the introduction which will tell us how to reverse a list in python.
What Reverse a List Actually Means:
While solving some of the complex problems in programming, we find it is easier to solve a problem using a python reverse list.
And using python reverse list we find that it makes our problem easier and saves our time and space complexities as well if we use it in an effective manner.
So let’s see what the python reverse list is –
List –
p | r | e | p | b | y | t | e | s |
---|
Reversed List –
s | e | t | y | b | p | e | r | p |
---|
The last indexed element comes first and the first indexed element goes to last.
But the main question is how to reverse a list in python so continue reading this article and now we will see some of the different ways in which python reverse list takes place.
Type 1 – Using reverse() method:
How to reverse a list in python using the reverse() method?
The reverse() method is one of the widely used inbuilt function to reverse a list. It is a built-in function of python.
It is one of the simplest way to reverse a list in python.
Syntax
List_Name.reverse()
Here, List_Name is the name of the python list which is going to be reversed.
Let’s see the implementation of the reverse() method with the help of an example-
Code Implementation:
# list name List_Name = ['p','r','e','p','b','y','t','e','s'] print(List_Name) #print actual list #use reverse() function List_Name.reverse() #printig the reverse list print("Using reverse() ", List_Name)
Output:
['p', 'r', 'e', 'p', 'b', 'y', 't', 'e', 's']
Using reverse() ['s', 'e', 't', 'y', 'b', 'p', 'e', 'r', 'p']
Time Complexity: O(N)
Space Complexity: O(1)
Where the length of the list is N.
-
reverse() function directly change the original list so it returns None. Let’s see this with the help of an example given below-
Code Implementation:
# list name List_Name = ['p','r','e','p','b','y','t','e','s'] print("Original list", List_Name) #print actual list #use reverse() function New_List = List_Name.reverse() #printig the reverse list print("stored reverse list result - ", New_List)
Output:
Original list ['p', 'r', 'e', 'p', 'b', 'y', 't', 'e', 's'] stored reverse list result - None
So, from above implementation we see that the reverse() function reverse the list in-place.
Type 2 – Using reversed() method:
How to reverse a list in python using the reversed() method?
The reversed() is a built-in function used to reverse a list in python but it will return an iterator. The iterator iterates in the reverse order.
Reverse iteration is supported by this built-in function, which was created specifically for this purpose.
Syntax
reversed(list_name)
list_name is the name of the list variable which is going to be reversed and it is passed as an argument to this built-in function.
Now let’s see this with the help of the example given below-
Code Implementation:
list_name = ['p','r','e','p','b','y','t','e','s'] for element in reversed(list_name): print(element,end=" ")
Output:
s e t y b p e r p
Type 3 – Slicing Method:
How to reverse a list in python using the slicing method?
The third approach to reverse a list in python is to use the slicing syntax. Slicing is one of the coolest features of python.
Its implementation is easy and quick.
Syntax:
list_name[start: stop: step]
To reverse the list we have to use -1 in place of step(by default it is 1).
Let’s see it with the help of an example given below:
list_name = ['p','r','e','p','b','y','t','e','s']
print(list_name[::-1])
Output:
['s', 'e', 't', 'y', 'b', 'p', 'e', 'r', 'p']
Time Complexity: O(N)
Space Complexity: O(N)
So, we see that in python reverse list slicing is one of the important technique.
- In [start: stop: step] if -1 is passed as a step parameter then the list will be reversed.
Type 4 – Two Pointer Approach:
How to reverse a list in python using the two-pointer approach?
It is one of the optimized approaches to the python reverse list.
In this method, two pointers will be initialized one at the start of the list and one at the end of the list.
Let’s name those pointers ‘left’ and ‘right’.
Steps to use this approach:-
- Run a loop until left< right and in each loop following things will take place –
- Swap elements of the left index and right index
- Increment the left pointer to 1 index
- Decrement the right point to 1 index
Dry Run with the help of an example:-
Let’s understand more clearly with the help of another coded example
# Reversing a list using two-pointer approach def reverse(arr): left = 0 right = len(arr)-1 while (left < right): # Swap temp = arr[left] arr[left] = arr[right] arr[right] = temp left += 1 right -= 1 return arr list_name = ['p','r','e','p','b','y','t','e','s'] print(reverse(list_name))
Output:
['s', 'e', 't', 'y', 'b', 'p', 'e', 'r', 'p']
Time Complexity: O(N)
Space Complexity: O(1)
Conclusion:
- List in python can be reversed in multiple ways.
- We can use any method based on the need of the program
- We saw reverse() and reversed() in-built functions.
- We can use 2 pointer approach
- Since some methods take extra memory and some will take constant memory so we will use methods like reverse() and two pointer approach to saving space complexity.
- As reverse() is easier to implement as compared to two pointer approach implementation so the reverse() method will be our top choice to implement the python reverse list.
Hence, in this article, we see what is python reverse list and saw multiple techniques to reverse a list in python. And find out that the reverse() method is one of the best method to reverse a list in python which is the most optimized and easy to implement.