Python Reference
list.insert()
Insert a single item at a specific index. Shifts elements to the right.
Last updated: October 2026
Definition: list.insert(index, item) inserts a single item at a specific index in a list. It shifts existing elements to the right and returns None. It never raises IndexError.
🎬 Live Example
Five common insert patterns. Click any button to see the output.
💡 insert is O(n) — all elements after the index shift right.
Basic Syntax
list.insert(index, item)
# index — position to insert at
# item — single object to insert
# Mutates list in place
# Returns None
# Never raises IndexError
🎯 Parameters & Return
| Parameter | Type | Description |
|---|---|---|
index | int | Position to insert. Negative counts from end. |
item | any | Any object. Inserted as a single element. |
| Return | None | Always None — mutates in place. |
Basic Insert
a = [1, 2, 3]
a.insert(1, 99)
# [1, 99, 2, 3]
fruits = ['apple', 'cherry']
fruits.insert(1, 'banana')
# ['apple', 'banana', 'cherry']
Insert at the Front
a = [2, 3, 4]
a.insert(0, 1)
# [1, 2, 3, 4]
# Same as unshift in other languages
# But O(n) — consider deque for frequent front inserts
Performance: For many front inserts, use collections.deque with appendleft() — it's O(1).
Negative Index
a = [1, 2, 3]
a.insert(-1, 99)
# [1, 2, 99, 3] ← inserted before the last
a.insert(-2, 88)
# [1, 2, 88, 99, 3]
Note: insert(-1, x) inserts before the last element, not after. To append, use append().
Out of Range Index
a = [1, 2, 3]
a.insert(100, 99)
# [1, 2, 3, 99] ← appends to end
a.insert(-100, 0)
# [0, 1, 2, 3] ← prepends
# insert NEVER raises IndexError
Real-World: Insert Sorted
def insert_sorted(lst, item):
"""Insert into a sorted list, keeping order."""
for i, existing in enumerate(lst):
if item < existing:
lst.insert(i, item)
return
lst.append(item)
nums = [10, 20, 30]
insert_sorted(nums, 25)
# [10, 20, 25, 30]
🎯 insert vs append vs extend
| Method | Adds | Complexity |
|---|---|---|
insert | 1 item at index | O(n) |
append | 1 item at end | O(1) |
extend | Many items at end | O(k) |
⚠️ Common Mistakes
- Assigning the return value.
a = a.insert(1, x)sets a to None. - Using insert for frequent front inserts. O(n) — use
collections.deque.appendleft(). - Confusing -1 with "after last".
insert(-1, x)inserts before the last item. - Inserting a list and expecting flat merge. insert adds as one nested element.
- Assuming IndexError. insert never raises — it clamps out-of-range indices.
❓ Frequently Asked Questions
What does insert() do in Python?
list.insert(index, item) inserts a single item at a specific index in a list. It shifts existing elements to the right and returns None.
What happens if the index is out of range?
insert never raises IndexError. A too-large index appends to the end. A negative index counts from the end, with -1 inserting before the last element.
What is the difference between insert and append?
append always adds to the end. insert adds at a specific index. append is O(1); insert is O(n) because it shifts elements.
What is the time complexity of insert?
insert is O(n) because all elements after the insertion point must shift right by one index. For very large lists, inserting at the front is slow.
Does insert return the list?
No. insert returns None. It mutates the list in place. Assigning the result sets the variable to None.