Python List - Insert Item/Element at Specific Position


Python - Insert Item at Specific Index in List

To insert or add an item at a specific position or index in a list, you can use the insert() method of the List class.

In this tutorial, we shall learn how to insert an item in a list, at a given position, with the help of example Python programs.


Syntax of insert()

The syntax of insert() method in the List class is:

mylist.insert(index, item)

The items present from the specified index are shifted right, and the specified item is inserted at the index.


Examples

1. Insert given item at a specified index in the list

In the following example, we have a list of numbers. We will insert an item 36, in the list at index 4.

Python Program

mylist = [21, 5, 8, 52, 21, 87, 52]
item = 36
index = 4

# Insert item in mylist at index
mylist.insert(index, item)

print(mylist)

Explanation

  1. The insert() method is called on the list mylist.
  2. The item 36 is inserted at the specified index 4.
  3. All elements from index 4 onward are shifted to the right.
  4. The resulting list is printed, showing [21, 5, 8, 52, 36, 21, 87, 52].

Output

[21, 5, 8, 52, 36, 21, 87, 52]

2. Insert given item at the beginning of the list

In this example, we will insert 36, at the start of the list. To insert at the start, provide the index as 0 to insert().

Python Program

mylist = [21, 5, 8, 52, 21, 87, 52]
item = 36
index = 0 # 1st position

# Insert item in mylist at index
mylist.insert(index, item)

print(mylist)

Explanation

  1. The index provided to insert() is 0, so 36 is added at the start of the list.
  2. All other elements are shifted to the right to accommodate the new item.
  3. The final list is [36, 21, 5, 8, 52, 21, 87, 52].

Output

[36, 21, 5, 8, 52, 21, 87, 52]

3. Insert given item at the end of the list

We will insert an item at the end of the list. To do this, provide the index as the length of the list to insert().

Python Program

mylist = [21, 5, 8, 52, 21, 87, 52]
item = 36
index = len(mylist)

# Insert item in mylist at index
mylist.insert(index, item)

print(mylist)

Explanation

  1. The index is specified as len(mylist), which points to the end of the list.
  2. The item 36 is appended to the end of the list.
  3. The updated list becomes [21, 5, 8, 52, 21, 87, 52, 36].

Output

[21, 5, 8, 52, 21, 87, 52, 36]

4. Insert item with an index out of bounds

If the index provided to insert() is greater than the length of the list, the item is appended to the end of the list.

Python Program

mylist = [21, 5, 8, 52, 21, 87, 52]
item = 36
index = 1000 # Index out of bounds

# Insert item in mylist at index
mylist.insert(index, item)

print(mylist)

Explanation

  1. The index 1000 is greater than the length of the list.
  2. In such cases, the insert() method appends the item to the end of the list.
  3. The final list becomes [21, 5, 8, 52, 21, 87, 52, 36].

Output

[21, 5, 8, 52, 21, 87, 52, 36]

5. Insert an item with a negative index

If a negative index is provided, the item is inserted relative to the end of the list. For instance, -1 inserts the item before the last element.

Python Program

mylist = [21, 5, 8, 52, 21, 87, 52]
item = 36
index = -1 # Insert before the last element

# Insert item in mylist at index
mylist.insert(index, item)

print(mylist)

Explanation

  1. The negative index -1 inserts the item before the last element.
  2. All elements starting from the index are shifted to the right.
  3. The resulting list is [21, 5, 8, 52, 21, 87, 36, 52].

Output

[21, 5, 8, 52, 21, 87, 36, 52]

Summary

In this tutorial of Python Examples, we learned how to insert an item at a given position in a list using the insert() method and covered various scenarios like adding items at the start, end, or with out-of-bounds and negative indices.




Python Libraries