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.Table of ContentUsing Negative Indexing (In-Place)Using a While Loop with List Indexing (In-Place)Using a StackUsing a For Loop with Negative IndexingReversing Using Two ListsReversing Using List Unpacking (Recursive)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 StackA 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 IndexingThis 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 ListsWe 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] Comment More infoAdvertise with us Next Article Reverse a list without using built-in functions P pragya22r4 Follow Improve Article Tags : Python Python Programs python-list Python list-programs python +1 More Practice Tags : pythonpythonpython-list 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 Python - Reverse Range in String List Given a string list, reverse each element of string list from ith to jth index. Input : test_list = ["Geeksforgeeks", "Best", "Geeks"], i, j = 1, 2 Output : ['ee', 'es', 'ee'] Explanation : Range of strings are extracted. Input : test_list = ["Geeksforgeeks"], i, j = 1, 7 Output : ['eeksfor'] Explan 3 min read Python | Reverse each tuple in a list of tuples Given a list of tuples, write a Python program to reverse each tuple in the given list of tuples. Examples: Input : [(1, 2), (3, 4, 5), (6, 7, 8, 9)] Output : [(2, 1), (5, 4, 3), (9, 8, 7, 6)] Input : [('a', 'b'), ('x', 'y'), ('m', 'n')] Output : [('b', 'a'), ('y', 'x'), ('n', 'm')] Method #1 : Nega 5 min read Python Program For Reversing A Doubly Linked List Given a Doubly Linked List, the task is to reverse the given Doubly Linked List. See below diagrams for example. (a) Original Doubly Linked List (b) Reversed Doubly Linked List Here is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes 4 min read Shift from Front to Rear in List - Python The task of shifting the first element to the rear in a list in Python involves moving the first element of the list to the end while maintaining the order of the remaining elements. For example, given the list a = [1, 4, 5, 6, 7, 8, 9, 12], the goal is to produce [4, 5, 6, 7, 8, 9, 12, 1].Using col 3 min read Like