In this section we will learn what the List insert() method is and how to use it in Python.
Python Insert into List: insert() Method
The Python list insert() method is used to insert a new value to a list object.
List insert() Method Syntax:
list.insert(index, value)
List insert() Method Parameter:
The method takes two arguments:
-
The first argument is the index position where we want to insert
the new value. Note that if there’s a value already in the specified
index, that element and any element afterward will be pushed one level
forward so the new value could be added without the removal of any of
the current elements in the list. -
The second argument is the value we want to insert to the
list.
List insert() Method Return Value:
The method does not return a value.
Example: insertion in array
li = ["ItemOne","ItemTwo","ItemThree","ItemOne","ItemTwo","ItemThree"]
li.insert(0, "ItemZero")
print(li)
Output:
['ItemZero', 'ItemOne', 'ItemTwo', 'ItemThree', 'ItemOne', 'ItemTwo', 'ItemThree']