How to Use the insert() Function in Python

Need to slot an item into the middle of a list? The insert function in Python does it with list.insert(index, item): the item lands at that index and everything after it moves one place to the right.

It edits the list in place, returns None, and quietly handles indexes that are too big or too small.

In this tutorial, I’ll walk you through the index rules, negative positions, adding several items, sorted inserts and speed on one road-trip list. I ran every snippet on Python 3.12.5 and NumPy 2.5.3.

The sample trip list we’ll use with insert()

Every example starts from this four-stop Route 66 plan, heading west:

stops = ["Chicago", "St. Louis", "Tulsa", "Amarillo"]
  • Index 0 is Chicago and index 3 (or -1) is Amarillo
  • Springfield, Illinois belongs between Chicago and St. Louis
  • Oklahoma City belongs between Tulsa and Amarillo
  • Santa Monica, the end of the road, is missing from the end

Those gaps are where people trip up. If you’re new to lists, start with creating a list in Python.

Method 1: Insert an item at a specific index with list.insert() (Recommended)

This is the everyday use: position first, item second, and Python makes room.

Same trip list here. Here’s the code:

stops = ["Chicago", "St. Louis", "Tulsa", "Amarillo"]

result = stops.insert(1, "Springfield")
print(stops)
print("insert() returned:", result)

stops.insert(0, "Milwaukee")              # index 0 = the front
stops.insert(len(stops), "Albuquerque")   # len() = the end
print(stops)
print(len(stops), "stops")

Output:

['Chicago', 'Springfield', 'St. Louis', 'Tulsa', 'Amarillo']
insert() returned: None
['Milwaukee', 'Chicago', 'Springfield', 'St. Louis', 'Tulsa', 'Amarillo', 'Albuquerque']
7 stops

Springfield becomes index 1, Milwaukee goes to the front, Albuquerque goes to the end, and the trip grows to 7 stops.

VS Code output of Python list insert adding Springfield at index 1, Milwaukee at index 0 and Albuquerque at the end of a trip list
insert(1, …) puts Springfield second and returns None; index 0 adds to the front and len(stops) adds to the end.

How does this code work?

stops.insert(1, "Springfield") puts Springfield at index 1. St. Louis, Tulsa and Amarillo each shift one spot right, so nothing is overwritten.

The return value is None. The method changes stops itself instead of handing back a new list.

Index 0 always means the front. len(stops) means one past the last item, which is the end (the length is 6 at that moment; more in finding the size of a list).

Method 2: Use a negative index with insert()

Negative indexes count from the end, but with insert() they surprise people.

Back to the original four stops. Let me show you:

stops = ["Chicago", "St. Louis", "Tulsa", "Amarillo"]

stops.insert(-1, "Oklahoma City")   # lands BEFORE the last item
print(stops)
print("Oklahoma City is at index", stops.index("Oklahoma City"))

stops.append("Santa Monica")        # the real way to add at the end
print(stops[-2:])

Output:

['Chicago', 'St. Louis', 'Tulsa', 'Oklahoma City', 'Amarillo']
Oklahoma City is at index 3
['Amarillo', 'Santa Monica']

Oklahoma City lands at index 3, between Tulsa and Amarillo, not at the end.

VS Code output of Python insert with index -1 placing Oklahoma City before Amarillo, then append adding Santa Monica at the end
insert(-1, …) puts Oklahoma City before the last stop, Amarillo; append() is what adds Santa Monica at the end.

How does this code work?

Index -1 points at Amarillo. insert() always puts the new item before the item at that index, so Oklahoma City slides in ahead of it.

stops.index("Oklahoma City") confirms it returns 3. Looking up positions like this is covered in getting the index of an element in a list.

So here’s the catch: there’s no negative index that means “after the last item”. Use append() or insert(len(stops), item) for that.

What happens when the insert() index is out of range?

A lot of tutorials say a big index raises IndexError. It doesn’t; insert() clamps it to the list’s edges.

Same trip list again, now with two silly indexes:

stops = ["Chicago", "St. Louis", "Tulsa", "Amarillo"]

stops.insert(99, "Santa Monica")    # far past the end
stops.insert(-99, "Milwaukee")      # far before the start
print(stops)

try:
    print(stops[99])                # reading index 99 is different
except IndexError as err:
    print("IndexError:", err)

Output:

['Milwaukee', 'Chicago', 'St. Louis', 'Tulsa', 'Amarillo', 'Santa Monica']
IndexError: list index out of range

Santa Monica lands at the end, Milwaukee at the front, and only reading stops[99] fails.

VS Code output of Python insert with index 99 and -99 adding items at the end and front with no error, while reading stops[99] raises IndexError
insert(99, …) appends and insert(-99, …) prepends with no error; only reading stops[99] raises IndexError.

How does this code work?

Any index past the end is treated as the end, and any negative index past the start as 0, just like slices.

Reading stops[99] is a different operation, and it does raise IndexError: list index out of range. If you hit that one, see fixing list index out of range.

Pro Tip: because a bad index never fails, a typo like 30 instead of 3 silently appends. Check len() first when the position matters.

Why does insert() return None?

List methods that change the list in place return None on purpose. Trouble starts when you save that result: trip = stops.insert(1, "Springfield") leaves trip as None.

Call trip.append("Gallup") next and you get AttributeError: 'NoneType' object has no attribute 'append', even though stops did get Springfield.

The fix: call stops.insert(...) on its own line, then keep using stops. The same trap with append() shows up in adding elements to an empty list.

Method 3: Insert several items at once with slice assignment

Say you want Joplin and Miami, Oklahoma, between St. Louis and Tulsa. Passing a list to insert() looks right, but isn’t.

Same trip list as before. Here’s the code for both tries:

stops = ["Chicago", "St. Louis", "Tulsa", "Amarillo"]

stops.insert(2, ["Joplin", "Miami"])     # one item: a nested list
print(stops)
print(len(stops), "items")

stops = ["Chicago", "St. Louis", "Tulsa", "Amarillo"]
stops[2:2] = ["Joplin", "Miami"]          # two items, spliced in
print(stops)
print(len(stops), "items")

Output:

['Chicago', 'St. Louis', ['Joplin', 'Miami'], 'Tulsa', 'Amarillo']
5 items
['Chicago', 'St. Louis', 'Joplin', 'Miami', 'Tulsa', 'Amarillo']
6 items

The insert() version gives 5 items with a nested list; the slice version gives 6 flat stops.

VS Code output comparing Python insert with a list, which nests it as one item, to slice assignment stops[2:2] that adds two flat items
insert() adds the whole list as one nested item (5 items); stops[2:2] = […] splices in two separate stops (6 items).

How does this code work?

insert() always adds exactly one object. A list is one object, so ["Joplin", "Miami"] sits at index 2 as a single nested item.

stops[2:2] is an empty slice at position 2. Assigning a list to it splices each element in, and nothing is removed. Slices are explained in slicing lists in Python; for the end, appending multiple items is simpler.

Method 4: Insert into a sorted list with bisect.insort()

If the list must stay sorted, let the bisect module find the index and insert in one call.

Here the trip becomes mile markers from Chicago:

import bisect

miles = [0, 300, 690, 1050]           # mile markers, kept sorted
bisect.insort(miles, 200)
print(miles)

route = [{"mile": 0, "city": "Chicago"},
         {"mile": 300, "city": "St. Louis"},
         {"mile": 690, "city": "Tulsa"}]
new = {"mile": 200, "city": "Springfield"}
bisect.insort(route, new, key=lambda s: s["mile"])   # Python 3.10+
print([s["city"] for s in route])

Output:

[0, 200, 300, 690, 1050]
['Chicago', 'Springfield', 'St. Louis', 'Tulsa']

200 lands between 0 and 300, and Springfield ends up second in the route.

VS Code output of Python bisect.insort adding mile 200 into a sorted list and a Springfield stop into a route sorted by a key
bisect.insort puts 200 in the right spot, and the key argument sorts the dictionary stops b