Tutorial Study Image

Python insert()

The insert() function in python helps to insert the given element into the specified index of the list.

list.insert(i, elem) #where element may be string, number, object etc.

insert() Parameters:

The insert() function takes two parameters. If the element is inserted in the specified index, all the remaining elements are shifted to the right.

Parameter Description Required / Optional
index the index where the element needs to be inserted Required
element the element to be inserted in the list Required

insert() Return Value

This method doesn't return any value. It modifies the original list by adding elements to it. If the given index is zero the element is inserted at the first position, if it index is two the element is inserted after the second element that is its position will be the third position.

Examples of insert() method in Python

Example 1: How to insert an element to the List

# alphabets list
alphabets = ['a', 'b', 'd', 'e']

# 'c' is inserted at index 2
# the position of 'c' will be 3rd
vowel.insert(2, 'c')

print('Updated List:', alphabets)


Updated List: ['a', 'b', 'c', 'd', 'e']

Example 2: How to insert a tuple as element to the List

firs_list = [{1, 2}, [5, 6, 7]]

# ex tuple
ex_tuple = (3, 4)

# inserting a tuple to the list
firs_list.insert(1, ex_tuple)

print('Final List:', firs_list)


Final List: [{1, 2}, (3, 4), [5, 6, 7]]