Python Prepend to List : 6 Different Easier Methods


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.

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.

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.

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
deque_converted = deque(list_example)
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.

for i in range(len(list_example)+1):
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
arr = np.array(list_example)
arr = np.insert(arr, 0, 1)
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 ).


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.


Source link

Leave a Reply

Your email address will not be published.