Open In App

Python List insert() Method With Examples

Last Updated : 01 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Python List insert() method inserts an item at a specific index in a list.

Example:

Python3




# creating a list
fruit = ["banana","cherry","grape"]
fruit.insert(1,"apple")
print(fruit)


Output

['banana', 'apple', 'cherry', 'grape']

Definition and Use of List insert() Method

List insert() method in Python is very useful to insert an element in a list. What makes it different from append() is that the list insert() function can add the value at any position in a list, whereas the append function is limited to adding values at the end.

It is used in editing lists with huge amount of data, as inserting any missed value in that list is made very easy with this Python function.

List insert() Method Syntax

list_name.insert(index, element)

Parameters:

  • index: the index at which the element has to be inserted.
  • element: the element to be inserted in the list.

Return : The insert() method returns None. It only updates the current list.

How to Insert into Python List at Index?

Using Python list insert() function you can easily insert an item to a given index in Python list.

Example

Python3




# making a list
score = [43,45,99,76]
#inserting a new score at third position
score.insert(2, 45)
#printing new list
print(score)


Output

[43, 45, 45, 99, 76]

More Examples on Python List insert() Method

Here is another examples to depict Python list insert at index 0:

Python3




list = ['Sun', 'rises', 'in', 'the', 'east']
list.insert(0, "The")
print(list)


Output

['The', 'Sun', 'rises', 'in', 'the', 'east']

Let’s see some of the scenarios with the python list insert() function to clearly understand the workings of the insert() function.

1. Inserting an Element to a specific index into the List

Here, we are inserting 10 at the 5th position (4th index) in a Python list.

Python3




list1 = [ 1, 2, 3, 4, 5, 6, 7
  
# insert 10 at 4th index 
list1.insert(4, 10
print(list1) 


Output

[1, 2, 3, 4, 10, 5, 6, 7]

2. Error of insert() Method

Here, we are inserting 1 at the 10th position in a Python list, we will get an error if we try to insert anything in a string because the string doesn’t have attribute insert().

Python3




# attribute error
string = "1234567"
  
string.insert(10, 1)
print(string)


Output: 

Traceback (most recent call last):
  File "/home/2fe54bd8723cd0ae89a17325da8b2eb5.py",
   line 7, in 
    string.insert(10, 1)
AttributeError: 'str' object has no attribute 'insert'


3. Insertion in a List Before any Element

In the parse of Python List Insert here, we are inserting 13 at the 3rd position before 3 in a Python list.

Python3




# Python3 program for Insertion in a list  
# before any element using insert() method 
  
list1 = [ 1, 2, 3, 4, 5, 6 ]
  
# Element to be inserted 
element = 13 
  
# Element to be inserted before 3
beforeElement = 3 
  
# Find index
index = list1.index(beforeElement) 
  
# Insert element at beforeElement 
list1.insert(index, element) 
print(list1)


Output

[1, 2, 13, 3, 4, 5, 6]

4. Inserting a Tuple into the List

Here we are inserting a tuple in a list using the insert() function in Python.

Python3




list1 = [ 1, 2, 3, 4, 5, 6 ]
  
# tuple of numbers
num_tuple = (4, 5, 6)
  
# inserting a tuple to the list
list1.insert(2, num_tuple)
  
print(list1)


Output

[1, 2, (4, 5, 6), 3, 4, 5, 6]

5. Insert an Element to the Beginning of a List 

In this example, we are inserting the “orange” string at the 0 index of the fruits list.

Python3




fruits = ['apple', 'banana', 'cherry']
fruits.insert(0, 'orange')
print(fruits)
 # Output: ['orange', 'apple', 'banana', 'cherry']


Output

['orange', 'apple', 'banana', 'cherry']

6. Inserting an Element at the end of the List

In this example, we are inserting the “cherry” at the end of the list.

Python3




fruits = ['apple', 'banana', 'cherry']
fruits.insert(-1, 'orange')
print(fruits) 
# Output: ['apple', 'banana', 'orange', 'cherry']


Output

['apple', 'banana', 'orange', 'cherry']

7. Inserting a dictionary to a list in Python

Here we are inserting a dictionary in a list using the insert() function in Python.

Python3




my_list = [{'name': 'Alice', 'age': 30}, 
           {'name': 'Bob', 'age': 25}]
new_dict = {'name': 'Charlie', 'age': 40}
  
my_list.append(new_dict)
  
print(my_list)


Output

[{'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 25}, {'name': 'Charlie', 'age': 40}]

8. Python Insert List in Another List

Here we are inserting a list in a list using the insert() function in Python.

Python3




list1 = [1, 2, 3]
list2 = [4, 5, 6]
  
list1=list1+list2
  
print(list1)


Output

[1, 2, 3, 4, 5, 6]

9. Insert elements of a set to a list in Python

Here we are inserting a set in a list using the insert() function in Python.

Python3




list1 = [1, 2, 3]
s= {4,5,6}
  
list1.insert(3,s)
  
print(list1)


Output

[1, 2, 3, {4, 5, 6}]

Go to the below articles to get more details information about Python Insert() Function

In the above article, we have discussed the Python list insert() method and its parameters with suitable examples. Python insert() function is very useful when dealing with big data.

We hope this article taught you about how to use insert() in Python.



Previous Article
Next Article

Similar Reads

Python | Insert list in another list
The problem of inserting a number at any index is a quite common one. But sometimes we require to insert the whole list into another list. These kinds of problems occur in Machine Learning while playing with data. Let's discuss certain ways in which this problem can be solved. Method #1: Using insert() + loop In this method, we insert one element b
8 min read
Python | Insert the string at the beginning of all items in a list
Given a list, write a Python program to insert some string at the beginning of all items in that list. Examples: Input : list = [1, 2, 3, 4], str = 'Geek' Output : list = ['Geek1', 'Geek2', 'Geek3', 'Geek4']Input : list = ['A', 'B', 'C'], str = 'Team' Output : list = ['TeamA', 'TeamB', 'TeamC'] There are multiple ways to insert the string at the be
3 min read
Python program to insert an element into sorted list
Given a sorted list and an element, Write a Python program to insert the element into the given list in sorted position. Examples: Input : list = [1, 2, 4], n = 3 Output : list = [1, 2, 3, 4] Input : list = ['a', 'b', 'c', 'd'], n = 'e' Output : list = ['a', 'b', 'c', 'd', 'e'] Approach #1 : This approach is the brute force method. Since the list i
9 min read
Python - Insert after every Nth element in a list
Sometimes we need to perform a single insertion in python, this can be easily done with the help of the insert function. But sometimes, we require to insert in a repeated manner after every n numbers, for that there can be numerous shorthands that can be quite handy. Lets discuss certain ways in which this can be done. Method #1 : Using join() + en
7 min read
Python | Split given list and insert in excel file
Given a list containing Names and Addresses consecutively, the task is to split these two elements at a time and insert it into excel. We can use a very popular library for data analysis, Pandas. Using pandas we can easily manipulate the columns and simply insert the filtered elements into excel file using df.to_excel() function. Below is the imple
1 min read
Python | Insert value after each k letters in given list of string
Given a list of string, write a Python program to Insert some letter after each k letters in given list of strings. As we know inserting element in a list is quite common, but sometimes we need to operate on list of strings by considering each letter and insert some repeated elements some fixed frequency. Let's see how to achieve this task using Py
5 min read
Python | Insert Nth element to Kth element in other list
Sometimes, while working with Python list, there can be a problem in which we need to perform the inter-list shifts of elements. Having a solution to this problem is always very useful. Let’s discuss the certain way in which this task can be performed. Method 1: Using pop() + insert() + index() This particular task can be performed using a combinat
9 min read
Insert Python list into PostgreSQL database
In this article, we will discuss how to Insert a Python list into PostgreSQL database using pyscopg2 module. Psycopg2 is the most popular PostgreSQL adapter for the Python programming language. Psycopg2 is a DB API 2.0 compliant PostgreSQL driver that is actively developed. It is designed for multi-threaded applications and manages its own connecti
2 min read
Insert and delete at the beginning in Doubly Linked List in Python
A doubly linked list is a collection of nodes. It is an advanced version of a singly linked list. In a doubly linked list, a node has three elements its data, a link to the next node, and a link to its previous node. In this article, we will learn how to insert a node at the beginning and delete a node from the beginning of the doubly linked list.
6 min read
List Methods in Python | Set 2 (del, remove(), sort(), insert(), pop(), extend()...)
Some of the list methods are mentioned in set 1 below List Methods in Python | Set 1 (in, not in, len(), min(), max()…) More methods are discussed in this article. 1. del[a : b] :- This method deletes all the elements in range starting from index 'a' till 'b' mentioned in arguments. 2. pop() :- This method deletes the element at the position mentio
4 min read
three90RightbarBannerImg