Python Prepend to List : 6 Different Easier Methods

Python Prepend to List

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)
prepend using insert
prepend using insert

 

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]=[1]
print(list_example)
prepend option using slicing
prepend option using slicing

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=[1]
list_example=temp_list+list_example
print(list_example)
addition property for prepend
addition property for prepend

 

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.

  1. Importing deque module from collection package.
  2. Converting a list to deque
  3. invoking appendleft()  function
  4. 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)
prepend using deque
prepend using deque

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)
prepend using for loop
prepend using for loop

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)
prepend using numpy
prepend using numpy

 

Conclusion –

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:

  1. 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.
  2. All of the above solutions will work but the most efficient is using insert() ( solution 1 ).

Thanks 

Data Science Learner Team

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner