The insert Method in Python: Inserting an Element into a List

The insert method adds a specified element before a specified index in a list. This method modifies the original list in place and returns None.

insert(i, x)

  • i -- position (index) before which the element is placed. Indexing starts at zero. Negative indexing is supported.
  • x -- element to be inserted into the list.
my_list = [1, 3]
my_list.insert(1, 2)  # my_list = [1, 2, 3]
my_list.insert(-1, 4)  # my_list = [1, 2, 4, 3]