Last Updated on April 28, 2023 by Prepbytes
A list in Python is a data type in which a user can add multiple items of different data types. The simple logic to find how to reverse a list in Python is that the last element of the list should be placed in the front position and the second last element of the list should be placed in the second position, and this continues with all the elements that are present in the list. We had built-in function reverse and reversed for reversing the list in python
A list is a significant and widely used data type in Python. The list may contain many values, some of which may be heterogeneous. Lists are ordered (meaning the components will stay in the same order as you have specified during a list declaration) and mutable (meaning internal values can be changed).
Its 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, The question “How to reverse 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 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 effectively.
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.
Approach 1 of How to reverse a list in Python using the reverse() method:
How to reverse a list in Python using the reverse() method?
The reverse() method is one of the widely used inbuilt functions to reverse a list. It is a built-in function of Python.
It is one of the simplest ways 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 how to reverse a list in Python with a 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) will be the time complexity to reverse a list in Python using the reverse method.
Space Complexity: O(1) will be the space complexity to reverse a list in Python using the reverse method.
Where the length of the list is N.
reverse() function directly changes 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 the above implementation, we see that the reverse() function reverses the list in place.
Approach 2 of How to reverse a list in Python using reversed() method:
Let’s see 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
Approach 3 of How to reverse a list in Python by using the slicing method:
Now, let’s see 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 techniques.
- In [start: stop: step] if -1 is passed as a step parameter then the list will be reversed.
Approach 4 of How to reverse a list in Python by using two pointer approach:
Let’s see our last approach to finding 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 of How to Reverse a list in Python
Let’s see the dry run of our two-pointer approach to find how to reverse a list in Python.
Let’s understand more clearly with the help of another coded example
Code Implementation
# 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:
A list in Python can be reversed in various ways, we had discussed 4 ways how to reverse a list in Python in this article. We have used built-in methods i.e. reverse() and reversed() to reverse the list in Python. Also, we had discussed the logic of how to reverse a list in Python with its dry run explaining the logic. We had seen the approach with the slicing method which is also a very crucial approach.
FAQs Related To How To Reverse a List in Python
Q1. How to reverse a list in Python without using the reverse () method?
Ans. To reverse a list without using the built-in reverse() function, we use the Slicing Operator. The slicing operator is another method used for reversing the data elements
Q2. Does reverse() work on strings?
Ans. reverse() works like reversing the string in place. However, what it does is create a new string containing the original data in reverse order.
Q3. What does reversed() return?
Ans. The reversed() function returns a reversed iterator object.
Q4. What does reverse() return?
Ans. The reverse function returns None as the reverse function do the changes in the original list and doesn’t make any copy for the updated list.