In this Blog Post, we will discuss how to reverse a list in python using for loop and alongside 4 more simple methods to reverse a list in python. All the approaches are super simple, I will walk you through all the steps with an in-depth guide and you’ll be good to go.
Well, as you know Lists are an essential data structure in Python, that offers great flexibility for storing and organizing elements. However, sooner or later you may need to flip it for some task and it is important for you to know how to perform list reversal efficiently and what are the ways that you can adapt. It’s one of those handy skills that you must be aware of, even if you are a programmer in general.
Reverse a List in Python using for loop (Append to New List)
In this approach, we will use a for
loop to reverse a list in Python. The basic idea is to iterate the list in a reverse manner from len — 1
index to 0
and as we iterate we need to append every element that we encounter into a new list that we must initialize before running the loop.
- We will initialize an empty list
reversed_list
that will hold our reverse items from the original list. - After that, we will iterate from the back of the list i.e.
len-1
index till the first element with0
index, while simultaneously appending every element into our empty resulting list withreversed_list.append(list[i])
.
This method is useful if you want to perform additional operations during the reversal process. See the implementation below:
list = [1, 2, 3, 4, 5]
reversed_list = [] # initializing resulting list
for i in range(len(list) - 1, -1, -1):
reversed_list.append(list[i])
print(f"Output: reversed_list")
Output: [5, 4, 3, 2, 1]
Using the ‘reverse()’ method in Python
In Python, there is a convenient built-in method called reverse()
that allows you to reverse the order of list items directly within the list itself.
It’s important to note that when using the reverse
method in Python, the original order of the list is altered. This means that the initial sequence of elements will be lost as a result of the reversal process. The code for this method is given below:
nums = [1, 2, 3, 4, 5]
nums.reverse()
print(f"Output: nums")
Output: [5, 4, 3, 2, 1]
Using list slicing method in Python
Also, if you have written some code in python you may know that python offers a list-slicing technique to clip out list elements according to our liking. Guess what? We can use this list-slicing technique in Python to reverse a list. By specifying [::-1]
we can create a new list with elements in reverse order. The original list remains unchanged.
Let’s take a look at an example below:
nums = [1, 2, 3, 4, 5]
reversed_list = nums[::-1]
print(f"Output: reversed_list")
Output: [5, 4, 3, 2, 1]
Using the ‘reversed()’ function in Python
reversed()
is a built-in function used to reverse an iterable object, such as a list, tuple, or string. It returns an iterator that produces the elements of the iterable in reverse order, so to convert that iterable into a list, we need to use list() along with it.
nums = [1, 2, 3, 4, 5]
reversed_list = list(reversed(nums))
print(f"Output: reversed_list")
Output: [5, 4, 3, 2, 1]
Using a While loop to swap elements while iterating
Using a while
loop we can reverse a list in Python by using the element-swapping approach. We are going to use the two-pointer method to initialize two pointers in our list: 1 at the first index & one at the last index.
Now, we will swap elements while simultaneously increasing the left pointer on the first index and decreasing the right pointer on the last index. We will continue doing this till left < right
. See the code below:
nums = [1, 2, 3, 4, 5]
left, right = 0, len(nums) - 1
while left < right:
# Swapping elements
nums[left], nums[right] = nums[right], nums[left]
left += 1 # Increasing left pointer
right -= 1 # Decreasing right pointer
print(f"Output: nums") # Output: [5, 4, 3, 2, 1]
Output: [5, 4, 3, 2, 1]
And there you have it, We discussed how to reverse a list in python using for loop, and 4 more techniques using a while loop, list slicing, reversed
function, and reverse
method. I hope you got a good idea of reversing list in python, try to implement these methods by yourself in VS Code and check if you have understood the intuition behind them.
StackOverflow Question: https://stackoverflow.com/questions/3940128/how-do-i-reverse-a-list-or-loop-over-it-backwards