The Python insert() method adds one item to a
list at a specified index. Existing items at and after that position move one place to the right.
Unlike append(), which always adds an item at the end, insert() lets us choose where the new item should be placed.
my_list.insert(index, item)
The insert() method accepts two arguments:
index - the position where the new item will be inserted.item - the value or object to add to the list.
Python lists use zero-based indexing, so index 0 refers to the first position.
In this example, 'Ravi' is inserted at index 1.
my_list=['Alex', 'Ronald', 'John']
my_list.insert(1, 'Ravi')
print(my_list)
Output
['Alex', 'Ravi', 'Ronald', 'John']
The item previously at index 1, 'Ronald', is not removed. It moves to index 2.
Use index 0 to insert a new item at the beginning of a list.
my_list=['Ronald', 'John']
my_list.insert(0, 'Alex')
print(my_list)
Output
['Alex', 'Ronald', 'John']
The existing items move one position to the right to make space for the new first item.
The len() function returns the number of items in a list. We can use that value as the index to insert a new item at the end.
my_list=['Alex', 'Ronald', 'John']
my_list.insert(len(my_list), 'Ravi')
print(my_list)
Output
['Alex', 'Ronald', 'John', 'Ravi']
This produces the same final position as append(). When the item simply needs to be added at the end, append() is clearer.
The insert() method also accepts negative indexes. A negative index counts positions from the end of the list.
my_list=['Alex', 'Ronald', 'John']
my_list.insert(-1, 'Ravi')
print(my_list)
Output
['Alex', 'Ronald', 'Ravi', 'John']
Index -1 refers to the position before the current last item, so 'Ravi' is inserted before 'John'.
If the supplied index is greater than the current list length, Python inserts the item at the end of the list.
my_list=['Alex', 'Ronald', 'John']
my_list.insert(100, 'Ravi')
print(my_list)
Output
['Alex', 'Ronald', 'John', 'Ravi']
Python does not create empty positions between the existing items and the new item. The value is simply placed at the end.
The insert() method modifies the existing list in place and returns None.
my_list=['Alex', 'John']
result=my_list.insert(1, 'Ronald')
print(result)
print(my_list)
Output
None
['Alex', 'Ronald', 'John']
Because insert() returns None, we normally call the method directly without assigning its return value.
my_list=['Alex', 'John']
my_list=my_list.insert(1, 'Ronald')
print(my_list)
Output
None
The original list was modified by insert(), but assigning its return value back to my_list replaces the variable with None.
Using insert() at an existing index adds a new item before the current item. It does not replace the value already stored at that index.
my_list=['Alex', 'Ronald', 'John']
my_list.insert(1, 'Ravi')
print(my_list)
Output
['Alex', 'Ravi', 'Ronald', 'John']
To replace the value at index 1 instead, assign a new value directly to that index.
my_list=['Alex', 'Ronald', 'John']
my_list[1]='Ravi'
print(my_list)
Output
['Alex', 'Ravi', 'John']
Both methods add an item to an existing list, but the position is handled differently.
insert(index, item) adds an item at a specified position.append(item) always adds an item at the end.my_list=['Alex', 'Ronald', 'John']
my_list.insert(1, 'Ravi')
print(my_list)
Output
['Alex', 'Ravi', 'Ronald', 'John']
my_list=['Alex', 'Ronald', 'John']
my_list.append('Ravi')
print(my_list)
Output
['Alex', 'Ronald', 'John', 'Ravi']
Use insert() when the new item's position matters. Use append() when the item should simply be added at the end.
The item being inserted can be a number or another Python object.
my_list=[1, 5, 2, 3]
my_list.insert(2, 22)
print(my_list)
Output
[1, 5, 22, 2, 3]
The value 22 is inserted at index 2. The value previously at that position moves to the right.
Adding one item with insert() increases the number of elements in the list by one.
my_list=['Alex', 'Ronald', 'John']
print("Length before insert:", len(my_list))
my_list.insert(1, 'Ravi')
print("Length after insert:", len(my_list))
print(my_list)
Output
Length before insert: 3
Length after insert: 4
['Alex', 'Ravi', 'Ronald', 'John']
insert() adds one item at a specified index.list.insert(index, item).0 inserts an item at the beginning.len(my_list) can be used to insert an item at the end.None.append() when the new item only needs to be added at the end.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.