Account
Categories

Python List insert()Method


Definition:

It inserts the value in the list at the index number, but it changes the shifting position of the data. When it inserts the data, the values shift to the right side.

Now you will clearly understand the method through the flowchart .


List →  a = [10, 20, 30]

Index :   0      1      2  
          ↓      ↓      ↓
Value :  10     20     30  

Method →  a.insert(1, 15)

Updated List →  [10, 15, 20, 30]

Index :   0      1      2      3  
          ↓      ↓      ↓      ↓
Value :  10     15     20     30  

Shift →          2030
                  →      →
               (right shift)

Syntax:

list_name.insert(index, value)

Example:

a = [10, 20, 30]

a.insert(1, 15)

print(a)

Output:

Exercise

Insert value 25 at index 2 in the list a = [5, 10, 20, 30] using Python.