Python List insert Function

Python insert List is one of the List functions used to insert a new item to the existing list at the user-specified index. In this section, we discuss how to use this Python List insert function with practical examples. The syntax of this list insert function is:

 list.insert(New_item, index_position)

This Python list insert method help us to add given item (New_item) at the index_position

Python insert List Function Example

The below list insert code inserts 50 at index position 2.

TIP: Please refer to List and List functions articles to understand everything about Lists in Python.

# Python Insert List Items

a = [15, 20, 35, 90]

print("Original List Items are : ", a)
a.insert(2, 50)
print("List Items are          : ", a)

List insert function output

Original List Items are :  [15, 20, 35, 90]
List Items are          :  [15, 20, 50, 35, 90]

insert List Function Example 2

In this program, we are inserting three values into an integer list.

# Python Insert List Items

a = [10, 20, 30, 40]

print("Original List Items are : ", a)
a.insert(1, 70)
a.insert(3, 50)
a.insert(0, 120)
print("List Items are          : ", a)

List insert function output

Original List Items are :  [10, 20, 30, 40]
List Items are          :  [120, 10, 70, 20, 50, 30, 40]

List insert Example 3

In this Python list insert function example, we declared a string list. Next, we used the insert function to insert a new item to this list at index position 1.

# Python Insert List Items

Fruits = ['Apple', 'Orange', 'Kiwi', 'Grape']

print("Original List Items are : ", Fruits)
Fruits.insert(1, 'Banana')
print("List Items are          : ", Fruits)

List insert function output

Original List Items are :  ['Apple', 'Orange', 'Kiwi', 'Grape']
List Items are          :  ['Apple', 'Banana', 'Orange', 'Kiwi', 'Grape']

List insert Function Example 4

This program is the same as the first example. However, this time, we are allowing the user to enter the length of a List. Next, we used For Loop to append those numbers to the list.

# Python Insert List Items

intList = []
 
number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    intList.append(value)
    
print("Original List Items are : ", intList)

item = int(input("Please enter the New Item to Insert : " ))
position = int(input("Please enter the New Item to Insert : " ))
intList.insert(position, item)
print("List Items are          : ", intList)
Python Insert List Items 4

List insert Function Example 5

This time, we used insert function on the Nested list (list inside a list).

# Python Insert List Items

MixList = [[71, 222], [222, 13], [14, 15], [99, 77]]
    
print("Original List Items are : ", MixList)
MixList.insert(2, [33, 55])
print("List Items are          : ", MixList)

List insert function output

Original List Items are :  [[71, 222], [222, 13], [14, 15], [99, 77]]
List Items are          :  [[71, 222], [222, 13], [33, 55], [14, 15], [99, 77]]

List insert Function Example 6

Let me use this list insert function on Mixed List.

# Python Insert List Items

MixList = ['apple',  1, 5, 'Kiwi', 'Mango']
    
print("Original List Items are : ", MixList)
MixList.insert(2, 'Banana')
print("List Items are          : ", MixList)

List insert function output

Original List Items are :  ['apple', 1, 5, 'Kiwi', 'Mango']
List Items are          :  ['apple', 1, 'Banana', 5, 'Kiwi', 'Mango']