Python List insert()


Python List insert() method

Python List insert() method inserts given object at specified index in the list.

The item that is already there at the specified index, and the items that are present after the specified index are shifted by one position right side.

https://youtu.be/kxdMTV7958Q?si=fzRoY-W6HSj7-VtH

In this tutorial, you will learn the syntax of, and how to use List insert() method, with examples.

Syntax of List insert()

The syntax of List insert() method is

list.insert(index, object)

You can read the above expression as: "In the list, insert at the index, the given object."

Parameters

extend() method can take one parameter. Let us see the parameter, and its description.

ParameterDescription
indexRequired
The index in the list at which we have to do the insert.
objectRequired
The Python object, that we need to insert.

Return value

insert() method returns None.

Please note that the insert() method modifies the original list.

Examples

1. Insert object 99 at index 3 in the list in Python

In the following program, we take a list my_list with some initial values. We have to insert the object 99 at index 3 in this list.

Call insert() method on the list my_list, and pass the index and object as arguments to the method.

Python Program

my_list = [11, 22, 33, 44, 55, 66]
my_list.insert(3, 99)
print(my_list)

Output

[11, 22, 33, 99, 44, 55, 66]

The object 99 is inserted in the list my_list at index 3.

Explanation

[11, 22, 33, 44, 55, 66]   ← given list
  0   1   2   3   4   5    ← indices
              ↑
         index = 3         ← insert object 99 at this index
[11, 22, 33,   , 44, 55, 66]
              ↑
             99

 [11, 22, 33, 99, 44, 55, 66]   ← resulting list

2. Insert object at the beginning of the list in Python

We can insert the given object at the beginning of the list by specifying index=0 for the insert() method, as shown in the following program.

We shall insert the object 99 at the beginning of the list.

Python Program

my_list = [11, 22, 33, 44, 55, 66]
my_list.insert(0, 99)
print(my_list)

Output

[99, 11, 22, 33, 44, 55, 66]

3. Insert object at the end of the list in Python

We can insert the given object at the end of the list by specifying index parameter of insert() method with length of the list, as shown in the following program.

We shall insert the object 99 at the end of the list.

Python Program

my_list = [11, 22, 33, 44, 55, 66]
my_list.insert(len(my_list), 99)
print(my_list)

Output

[11, 22, 33, 44, 55, 66, 99]

4. Insert object at index > length of the list in Python

If we specify an index to insert() method, that is greater than the length of the list, the object shall be appended to the list.

In the following program, we have taken a list with six objects. But we shall try to insert the object 99 at index 10.

Python Program

my_list = [11, 22, 33, 44, 55, 66]
my_list.insert(10, 99)
print(my_list)

Output

[11, 22, 33, 44, 55, 66, 99]

Summary

In this tutorial of Python Examples, we learned about List insert() method, how to use insert() method to insert an object in the list at specified index, with syntax and examples.

We have seen example programs on how to insert an object at specified index, at beginning of the list, or at the end of the list.