Python prepend to list is an operation where we add element(s) at the start of any python list. Typically insertion at the start is rare than the last position. If we simply perform the insertion of any element in the list or any data structure, we call it to append. Since List is a mutable python object so we can append and prepend easily. In this article, we will see the different ways to prepend a list.
Python prepend to list : ( Solutions )-
We will discuss most easiest ways to prepend. You may opt anyone to crack your code.
Solution 1 : Using insert() library Function –
This insert() is predefine function in the python list. Here we can define the index position where we want to insert and the second argument will be the element which we want to insert. Let’s see with an example, It will make you more clear.
list_example=[2,3,4,5] list_example.insert(0,1) print(list_example)
Solution 2: Utilizing Slicing indexes property –
Since List is a mutable object hence we can apply slicing indexes. Here we will use the same list to demonstrate the functionality.
list_example=[2,3,4,5] list_example[:0]= print(list_example)
Here we need to make sure that slicing supports an iterable object. Hence we need to provide element (1) in the list form.
Solution 3: Using the Addition operator ( Python List Property) –
In python, we can combine any two lists by the “+” operator. In this scenario, we will put the element which we want to insert in the first position, and then we will combine to a list. We need to assign the same to the new list as well. I think the below example is self-explanatory, So let’s go through the same.
list_example=[2,3,4,5] temp_list= list_example=temp_list+list_example print(list_example)
Solution 4 : Using appendleft() function from deque –
In the python collection module, The deque package has appendleft() function. This appendleft() function will insert the element at the first position. This is not complex but step by step understanding will make you more clear.
- Importing deque module from collection package.
- Converting a list to deque
- invoking appendleft() function
- deque data structure to list.
from collections import deque list_example=[2,3,4,5] deque_converted = deque(list_example) deque_converted.appendleft(1) list_appended=list(deque_converted) print (list_appended)
Solution 5: Using for loop –
This is a completely custom way of doing it. Here we will iterate the existing list using for loop in python. We will first append the new element in the new list followed by the element of the existing list. Here is the code for this.
list_example=[2,3,4,5] newlist= for i in range(len(list_example)+1): if(i==0): newlist.append(1) else: newlist.append(list_example[i-1]) print(newlist)
Solution 6: Using NumPy ( python prepend to NumPy array )-
In this approach, we will first convert our list to NumPy array and then we insert the element at the first position ( prepend to list ). After it, we will convert the NumPy array to a python list.
import numpy as np list_example=[2,3,4,5] arr = np.array(list_example) arr = np.insert(arr, 0, 1) newlist=arr.tolist() print(newlist)
The above tricks will help you append a string, float, list, etc to the python list. Although we have shown all the examples with an integer values. Here are a few important things, We would like to highlight:
- Appending operation is less computationally costly than prepend. This is because prepend requires inserting and shifting other elements. In the opposite side, append only require an insertion process.
- All of the above solutions will work but the most efficient is using insert() ( solution 1 ).
Data Science Learner Team
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.