Python list Method - insert()

The insert() method in Python is used to insert an element into a specified index of a list. The method modifies the original list in place and does not return anything.

The syntax for the insert() method is as follows:

list.insert(index, element)
So‮w:ecru‬ww.theitroad.com

Here, list refers to the list in which the element is to be inserted, index is the index at which the element needs to be inserted, and element is the object to be inserted.

Example:

# Creating a list
my_list = [1, 2, 3, 4]

# Inserting an element at index 2
my_list.insert(2, 5)

# Displaying the modified list
print(my_list)   # Output: [1, 2, 5, 3, 4]

In the above example, the insert() method is used to insert the integer 5 at index 2 of the list my_list. The original list is modified and the output shows the updated list.