Open In App

Reverse a list without using built-in functions

Last Updated : 02 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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)

Output
[5, 4, 3, 2, 1]

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)

Output
[1, 2, 3, 4, 5]

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)

Output
[5, 4, 3, 2, 1]

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)

Output
[5, 4, 3, 2, 1]

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)

Output
[5, 4, 3, 2, 1]

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)

Output
[5, 4, 3, 2, 1]

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)

Output
[5, 4, 3, 2, 1]

Next Article

Similar Reads