Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › Python › Reference › insert()

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.

Input:

                
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
indexintPosition to insert. Negative counts from end.
itemanyAny object. Inserted as a single element.
ReturnNoneAlways None — mutates in place.
1

Basic Insert

a = [1, 2, 3]
a.insert(1, 99)
# [1, 99, 2, 3]

fruits = ['apple', 'cherry']
fruits.insert(1, 'banana')
# ['apple', 'banana', 'cherry']
2

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).

3

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().

4

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
5

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
insert1 item at indexO(n)
append1 item at endO(1)
extendMany items at endO(k)

⚠️ Common Mistakes

❓ 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.

🎯 What's Next?

← All Python Reference