Reverse a list without using built-in functions
Last Updated :
02 Dec, 2024
Reversing a list in Python is a common task, but sometimes we might not want to use built-in functions like reverse() or slicing. Let’s first look at a simple example where we reverse a list by swapping elements:
Swapping Elements (In-Place)
The most efficient way to reverse a list is by swapping elements in place. This method doesn't require any extra space and changes the original list. Let's see how it works.
Python
a = [1, 2, 3, 4, 5]
start = 0
end = len(a) - 1
# Swap elements until start index crosses end index
while start < end:
a[start], a[end] = a[end], a[start]
start += 1
end -= 1
print(a)
Let's take a look at different approaches to reverse a list without using built-in functions.
Using Negative Indexing (In-Place)
Another simple method is to use Python's negative indexing to access elements from the end of the list. This method also reverses the list without using extra space.
Python
a = [1, 2, 3, 4, 5]
reversed_a = []
# Loop through the list in reverse order using negative indexing
for i in range(-1, -len(a) - 1, -1):
reversed_a.append(a[i]) # Add element to the new list
print(a)
Using a While Loop with List Indexing (In-Place)
We can also use a while loop to loop through the list backwards and append the elements to a new list. This method is similar to the negative indexing method but uses list indexing directly.
Python
a = [1, 2, 3, 4, 5]
reversed_a = []
i = len(a) - 1 # Start from the last index
# Loop until we reach the first element
while i >= 0:
reversed_a.append(a[i]) # Add element to the new list
i -= 1 # Move to the previous element
print(reversed_a)
Using a Stack
A stack is a data structure that works on the principle of Last In, First Out (LIFO). We can use a stack to reverse a list by pushing each element onto the stack and then popping them out in reverse order.
Python
a = [1, 2, 3, 4, 5]
stack = []
# Push each element to the stack
for item in a:
stack.append(item)
reversed_a = []
# Pop elements from the stack and add to the new list
while stack:
reversed_a.append(stack.pop()) # pop from stack and append to reversed_a
print(reversed_a)
Using a For Loop with Negative Indexing
This method is very similar to the negative indexing method, but it uses a for loop instead of a while loop. It’s another simple way to reverse a list.
Python
a = [1, 2, 3, 4, 5]
b = []
# Loop through the list in reverse using negative indexing
for i in range(-1, -len(a) - 1, -1):
b.append(a[i]) # Add the element at each negative index
print(b)
Reversing Using Two Lists
We can reverse a list by creating a new list and adding each element from the original list to the front of the new list.
Python
a = [1, 2, 3, 4, 5]
b = []
# Add each item to the front of reversed_a
for item in a:
b = [item] + b
print(b)
Reversing Using List Unpacking (Recursive)
This method uses recursion to reverse the list. It’s a bit more complex, but it shows how recursion can be used for problems like this.
Python
def reverse(a):
if not a:
return a
return reverse(a[1:]) + [a[0]] # Reverse the rest of the list and add the first element at the end
a = [1, 2, 3, 4, 5]
b = reverse(a)
print(b)
Similar Reads
Reverse a List in Python Without Using reverse() Function While reverse() is the easiest and most direct method to reverse a list in Python it's useful to know alternative approaches to reverse a list without relying on this function. Using SlicingWe can use Pythonâs list-slicing feature to create a reversed list version without modifying the original one.
2 min read
Reverse All Strings in String List in Python We are given a list of strings and our task is to reverse each string in the list while keeping the order of the list itself unchanged. For example, if we have a list like this: ['gfg', 'is', 'best'] then the output will be ['gfg', 'si', 'tseb'].Using For LoopWe can use a for loop to iterate over th
2 min read
How To Reverse A List In Python Using Slicing In Python, list slicing is a common practice and it is the most used technique for programmers to solve efficient problems. In this article, we will see how we can reverse a list using slicing in Python. Reverse a List Using Slicing in PythonBelow are some of the examples by which we can perform rev
3 min read
Python Program For Printing Reverse Of A Linked List Without Actually Reversing Given a linked list, print reverse of it using a recursive function. For example, if the given linked list is 1->2->3->4, then output should be 4->3->2->1.Note that the question is only about printing the reverse. To reverse the list itself see this Difficulty Level: Rookie Algorit
2 min read
How to Reverse a Dictionary in Python Reversing a dictionary typically means swapping the keys and values, where the keys become the values and the values become the keys. In this article, we will explore how to reverse a dictionary in Python using a variety of methods.Using Dictionary ComprehensionDictionary comprehension is a concise
2 min read
Python | Reverse sign of each element in given list Given a list of integers, write a Python program to reverse the sign of each element in given list. Examples: Input : [-1, 2, 3, -4, 5, -6, -7] Output : [1, -2, -3, 4, -5, 6, 7] Input : [-5, 9, -23, -2, 7] Output : [5, -9, 23, 2, -7] Methods #1: List comprehension Python3 # Python3 program to Conver
3 min read