Sometimes, you might want to use your own method in order to reverse a list in Python rather than using inbuilt functions. In this short article, we will learn how to reverse list in Python without inbuilt functions. We will go through various methods and explain each one in detail so that beginners will understand the reversing of lists in Python without using inbuilt methods.
Reverse List in Python Without Using Inbuilt Function
Reverse list in Python means to change the position of the elements in such a way that the last element takes the first place, the second last element takes the second position, and so on. The following diagram shows how the reverse list in Python works:
As you can see, the order of the list has been changed after applying the reversing. Now, we will go through various methods through which we can reverse the order of the list without using the built-in methods
Method-1: Using Slicing
If you are using Python for a long time, you might have been through the slicing method. If not, it is still fine. We will explain briefly what is slicing in Python and how we can use slicing in order to reverse a list in Python.
Let us first take an example of slicing to reverse the list in Python and then we will explain the code in detail so that it will be easy for beginners to understand the code.
# list list1 = [1,2,3,4,5,6] # Reverse List in Python Without Using inbuilt function reversed_list = list1[::-1] print (reversed_list)
[6, 5, 4, 3, 2, 1]
As you can see, the list has been reversed and also we didn’t use any inbuilt methods. Now let us explain the code step by step.
list1[::-1]uses slicing notation to create a reversed copy of the list. The syntax
[start:stop:step]specifies a range of items in a list. The
stopindices are not specified in this case, so the slice includes all the items in the list. The
stepis set to
-1, which means that the slice starts from the end of the list and goes backwards by one index at a time.
- The resulting list is assigned to the variable
- Finally, the
print()function is used to display the
reversed_liston the console. The output will be
[6, 5, 4, 3, 2, 1], which is the original list in reverse order.
Method-2: Using a User-Defined Function
A User defined function in Python is a function that is created by the user in order to perform specific tasks. In this case, we will create a function that will help us to find the reverse list in Python.
Let us first create a function and then we will explain the code in detail.
# function def reverse_list(l): reversed_list = l[::-1] return reversed_list # calling the function list1 = [1,2,3,4,5,6] print(reverse_list(list1))
[6, 5, 4, 3, 2, 1]
Notice that the list has been reversed by the function. Let us explain the function in detail so that beginners will understand the function.
- We define a function named
reverse_listthat takes a list
- Inside the function, we create a new list named
reversed_listthat contains the elements of the input list
lin reverse order. We do this by using the slice notation
[::-1], which starts at the end of the list and moves backward through it one element at a time.
- We then return the reversed list
- Outside of the function, we create a list named
list1with some integer elements.
- We then call the
reverse_listfunction and pass the
list1list as an argument.
- The function returns a new list
reversed_list, which we then print using the
- When the code is executed, the output is the reversed list
[6, 5, 4, 3, 2, 1].
Method-3: Using For Loop
A for loop in Python is a type of loop that iterates through an object for a specific time. Mostly, we specified the number of iterations before starting the for loop. We can use the for loop to iterate through the list and find the reverse of the list.
Let us first go through the example and see how we can use the for loop in Python to find the reverse of the list and then we will explain the code.
# reverse list in python def reverse_list_in_place(l): for i in range(len(l)/2): beginning = i end = -i-1 temp = l[end] l[end] = l[beginning] l[beginning] = temp return l # calling the function list1 = [1,2,3,4,5,6] print(reverse_list_in_place(list1))
[6, 5, 4, 3, 2, 1]
As you can see, we got the reverse of the list without using any inbuilt methods. Now, let us explain the code in detail so that it will be easy for beginners to understand code.
This code defines a Python function
reverse_list_in_place that takes a list
l as input and returns the same list with its elements reversed. Here’s a detailed breakdown of the code:
This line defines the function reverse_list_in_place which takes a list l as its argument.
for i in range(len(l)/2):
This line starts a loop that iterates over half of the length of the input list. This is because we only need to swap the first half of the list with the second half to reverse the list.
beginning = i end = -i-1
These two lines assign variables beginning and end to the indices at the beginning and end of each pair of elements that need to be swapped.
Note that end is assigned to -i-1 to get the index of the corresponding element from the end of the list.
temp = l[end] l[end] = l[beginning] l[beginning] = temp
These three lines swap the elements at the
end indices using a temporary variable
temp. This is done for each pair of elements in the list, starting from the outermost pairs and moving toward the center.
And then we are returning the l as a reversed list. Hope this explanation will help you to understand how we are reversing the list in Python without using inbuilt functions.
In this short article, we discussed how we can reverse list in Python without using inbuilt functions. Mostly, people use the reverse() method to reverse the list in Python. But we can use our own method to find the reverse of the list in Python without using the inbuilt functions. In this article, we discussed three different methods to find the reverse list in Python without using any built-in methods.