What is List.insert() in Python?

In python, the List insert() method inserts the specified value at a given index in a list.

Syntax


list.insert(index, elmnt)

Parameter

The following are the required parameters:

  • index: specifies the position to insert the element.

  • elmnt: is the element to be inserted.

Code

The following code shows how to use the insert() method:

Example 1: A program that inserts an element into a list

even_no = [ 2, 4, 6, 8, 10 ]
# insert 0 at the front of the list
even_no.insert(0, 0)
print(even_no)
letters = ['a', 'b', 'c', 'd', 'e']
# insert f at 3th index
letters.insert(3, 'f')
print(letters)

Example 2: A program that inserts a tuple into a list

numbers = [ 7, 23, 1, 5, 16 ]
# tuple of numbers
num_tuple = (6, 8, 9)
# inserting a tuple to the list
numbers.insert(2, num_tuple)
print(numbers)

Free Resources