Prepend List in Python (Append at the beginning)

featured Image

In this article, we will learn how to prepend items in a list in Python. Python has an in-built method called append() to append (add at the end) items to a list.

However, there are no such in-built methods or functions to prepend (add at the beginning) items to a list in Python.

So let’s see what different methods we can use to prepend values in a Python list.

Prepend List in Python

To prepend list in Python we can use any of the following methods:

  1. Using + operator
  2. Using the insert() method.
  3. Using deque() function

Let’s see each of the methods with an example.

Using + operator to prepend an item to a Python List

The + operator is an arithmetic operator that is used to add values (operands) together in python.

This + operator can also be used to add two or more lists together into a single one. It merges the lists in the order we place them.

We can use the + operator to prepend multiple items to a Python list easily.

Let’s see an example below where we will prepend a list item with another one using the + operator.

Example:

color_list = ['red','green','yellow']

item = ['blue']

new_list = item + color_list

print(new_list)

Output:

['blue', 'red', 'green', 'yellow']

Here, we have used the + operator to add the item variable value at the beginning of the python list i.e color_list .

Prepend list using the insert() method in Python

The easy and simple way to prepend an item in a Python list is to use the insert() method.

The insert() method is used to insert values at a specific position in python.

Syntax:

list.insert(position,value)

position – The specific position (index) we want the item to be inserted.

value – The item we want to insert in our python list.

So, to add an item in front of the Python list, we will use the insert() method and pass on the position and the item as parameters.

my_list = ['red','green','yellow']

my_list.insert(0, 'blue')

print(my_list)

Output:

['blue', 'red', 'green', 'yellow']

Here, we have inserted the item ‘blue’ in the index position 0 in our Python list.

Prepend list in Python using deque function

Deque stands for Doubly Ended Queue. It is a queue in which we can insert and delete items from both the left and right ends of the queue.

It can be implemented on any list using the collections module.

A list in python only allows us to add items at the end of the list using the append() function. A Python list cannot make space at the front to add items.

However, a deque allows us to add and remove items from the end and the front of a list.

So, to prepend a list in Python using the deque function:

  1. Convert the Python list to deque using the deque() function
  2. Using the appendleft() method add the item at the beginning.
  3. Convert it back to a Python list using list() function.

Let’s see an example below.

from collections import deque

color_list = ['red','green','yellow']

deque_list =  deque(color_list)

deque_list.appendleft('blue')

print(list(deque_list))

Output:

['blue', 'red', 'green', 'yellow']

Here, we have converted the python list into a deque object and then we have to prepend the value “blue” to the list.

Conclusion:

Here, we have learned ways to prepend one or multiple values in a Python list using different methods. The simple and easy way is the use the insert method. However, if you want faster performance then convert the list into a deque object using the deque function.


Related Article:

Get the Index or Position of Item in List in Python

How to split a list into multiple list using python

Remove Duplicates from List in Python

Prepend List in Python

Python – Insert an element at specific index in a list

Python – Check if list is empty or not

Remove the Last N element from a List in Python

Related Posts

merge json files in python

How to merge JSON files in python

JSON (JavaScript Object Notation) is a data format used for storing, transferring, and visualizing data. Its simple structure of key:value pairs and array data types makes JSON an easy and…

Read more
Install python pip in ubuntu

How to Install Pip(pip3) in Ubuntu Linux 22.04

Pip is a package manager for Python that allows you to install and manage additional Python packages that are not part of the Python standard library. Pip makes it easy…

Read more
featured Image

How to Write If-Else Statements in One Line in Python

If-else statements are a fundamental concept in Python and other programming languages. They allow you to execute different blocks of code based on whether a condition is true or false….

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *