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
- The
insert()
method is called on the listmylist
. - The item
36
is inserted at the specified index4
. - All elements from index
4
onward are shifted to the right. - 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
- The index provided to
insert()
is0
, so36
is added at the start of the list. - All other elements are shifted to the right to accommodate the new item.
- 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
- The index is specified as
len(mylist)
, which points to the end of the list. - The item
36
is appended to the end of the list. - 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
- The index
1000
is greater than the length of the list. - In such cases, the
insert()
method appends the item to the end of the list. - 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
- The negative index
-1
inserts the item before the last element. - All elements starting from the index are shifted to the right.
- 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.