Python Prepend to List [Step by Step Guide To Add Elements in List]

Python Prepend to List [Step by Step Guide To Add Elements in List]

In this tutorial, we will learn about Python prepend to list using step by step guide. In Python, list is one of the many data structure which is widely used by developers. It provides many built-in functions (like append(), extend(), insert(), remove() etc) which makes our day to day programming tasks quite convenient. In today’s tutorial, we will specifically talk about prepending elements in an existing list using various methods. so let’s get started.

 

Python List Overview

In Python, list is a built-in data structure that allows us to store a collection of items of different data types. Lists are widely used data structure as they are mutable which means we can modify the existing list after its creation. It also allows us to hold elements of different data types in the same list (like int, string, list etc). For example, we have created a list in below code which holds 3 elements, int type, string type and list type. When we iterate over the list, it will return all the elements.

list1 = [4, 'linuxnasa', [1, 2, 3]]

for x in list1:
    print(x)
OUTPUT
4
linuxnasa
[1, 2, 3]

 

Python Prepend to List [Step by Step Guide To Add Elements in List]

In Python, you often have heard about the word ‘Append’ to the list. This means, we add one one or more elements at the end of an existing list. The term ‘Prepend’ is just opposite to ‘Append’. It refers to adding one or more elements at the beginning of an existing list (unlike append). This means, when you add elements at the start of a list, the existing items indices get shifted to higher indices of the list.  There are many ways to prepend element(s) in the existing list. We will discuss some of the most commonly used methods in this tutorial. So let’s get started.

 

Using ‘+’ Operator

The very common and basic method to prepend element(s) in an existing list is by using the ‘+’ operator. If you are familiar with string operations in Python, you must be knowing that ‘+’ operator is used to concatenate two strings. Similarly, we will use ‘+’ operator to concatenate two list. Let us look at the example below to understand how can we achieve prepend to list using ‘+’ operator.

Example-1

list1 = [7, 8, 9]
list2 = [2, 3, 4]

new_list = list2 + list1
print(f"New list after prepending list2 in list1: {new_list}")
OUTPUT
New list after prepending list2 in list1: [2, 3, 4, 7, 8, 9]

In the above example, we have created list1 which has the elements [7,8,9]. Next, we have created another list ‘list2’ which will be prepended to list1. The new list after prepend will first contain the elements of list2 followed by elements of list1.

 

 

Using insert() Function

In Python, insert() is a built-in list function which is used to add new elements in the existing list. This method provides the flexibility to add the elements anywhere in the existing list based on the index. So, we can utilize this function to prepend the element(s) at the start of an existing list. The way insert() function works is that it treats the element passed to its argument as single entity. By this what i mean is that if you pass list2 ([2,3,4]), it will be treated as single element and whole list will get added at a certain index in the existing list. Let us look at the example below to understand the implementation.

Example-2

list1 = [7, 8, 9]
list2 = [2, 3, 4]

list1.insert(0, list2)
print(f"New list1 after prepend: {list1}")
OUTPUT
New list1 after prepend: [[2, 3, 4], 7, 8, 9]
As you see the output above, we have prepended list2 in list1. list2 got added at index 0 in list1 followed by all elements of list1. If you want to individually add the list two elements at the beginning of list1, you have to pass each element of list2 separately in insert() function argument as shown in below example.

 

Example-3

list1 = [7, 8, 9]
list2 = [2, 3, 4]

index = 0
for i in range(len(list2)):
    list1.insert(index, list2[i])
    index = index + 1

print(f"New list1 after prepend: {list1}")
OUTPUT
New list1 after prepend: [2, 3, 4, 7, 8, 9]

 

Using List Comprehension

Another way to prepend elements in an existing list is by utilizing the list comprehension technique. We will understand how this method works by using below example.

Example-4

list1 = [7, 8, 9]
list2 = [2, 3, 4]

new_list = list2 + [x for x in list1]
print(f"New list1 after prepend: {new_list}")
OUTPUT
New list1 after prepend: [2, 3, 4, 7, 8, 9]
In the above example, we are creating a new list whole elements will be elements from list2 followed by list1 elements. We have first added elements of list2 in the new_list. Then, we have used ‘+’ operator to concatenate list2 to list1. Next, we are iterating elements of list1 and each element in list1 is getting added as new element in new_list.

 

Using deque Module

In Python, collections module provides many  data structure and one of them is known as ‘deque()‘  which is also used to prepend element at the starting of an existing list. The deque data structure provides a method called appendleft() that takes an element as argument and prepend to the existing list. Let us look at the example below to understand the implementation.

Example-5

from collections import deque

list1 = deque([7, 8, 9])
list1.appendleft(4)

print(f"New list1 after prepend: {list1}")
OUTPUT
New list1 after prepend: deque([4, 7, 8, 9])
In the above example, we have prepended 4 in the existing list list1 using deque() data structure. Similarly, you can pass any element as an argument to prepend in the existing list.

 

Summary

To learn more about all built-in list functions in Python, refer to python.org official document.

Leave a Comment