How to reverse a list in python without using reverse function


In this Python code snippet post, we are going to implement a list reverse function from the scratch without using any builtin functions.

Let us see how…

Iterative solution

We can use a for loop to swap the first and last items, the second and the one before the last item and so on until the list is reversed in place. The number of iterations needed is half the list size. If the list has an odd number of items then the middle item will stay in its place.

Here is an example Python code to do that

Recursion solution

We can write a recursive reverse function to reverse a list. The stopping condition occurs when the list is one item in length in such case we just return that list otherwise we return list concatenation constructed from the first item and the recursive reverse of the rest of the items. For example Reverse([1, 2, 3, 4]) = Reverse([2, 3, 4]) + [1].

Here is an example implementation…

I hope this post was useful. Thanks for visiting


Add a Comment

Your email address will not be published. Required fields are marked *