← All posts
November 15, 2026 · 3 min read

Array insertion time complexity, shown with lockers

An array keeps its slots side by side like a corridor of lockers. See why reading any item is one jump, and why an insert shifts everything after it.

Array insertion time complexity comes down to one picture: an array is a corridor of lockers, numbered from zero, standing side by side with no gaps. That one fact explains why a Python list can read its millionth item instantly, yet slows down when you insert something in the middle.

Six lockers numbered 0 to 5, side by side with no gaps, holding 7, 3, 9, 4, 1, 8, with the list written in Python below

What does "side by side" mean?

An array stores its items in one unbroken block of memory. Slot 0 comes first, slot 1 right after it, then slot 2, and so on, with nothing in between. In CPython, a list is exactly this: a block of slots, each 8 bytes on a 64-bit machine, and each slot holds a reference to one item.

In the video we start with six lockers:

lockers = [7, 3, 9, 4, 1, 8]

Why is reading any item one jump?

Every locker is the same size. So to find locker 4 you don't walk past lockers 0, 1, 2 and 3. You compute where it is: the start of the corridor plus 4 times the size of one locker.

Four hops from the start of the corridor to locker 4, labelled start + 4 × size

That is one multiplication and one addition, then one jump straight to the right address. It costs the same whether the list has six items or a million. That's what O(1) means: the work doesn't grow with the size of the input.

One arrow from the start straight to locker 4: one jump even with a million lockers, O(1)

lockers[4] is the fifth locker (we count from zero), and it holds 1.

Why does inserting in the middle cost more?

Now put a 5 into locker 2. There is no gap to slide it into: locker 2 already holds 9, and the lockers after it are full too. So every item from locker 2 onward has to move one door to the right, and we need one extra locker at the end to make room.

The order matters. If you started at locker 2, you would overwrite 4 with 9 before moving the 4 anywhere. So you shift from the end backwards: the 8 moves from locker 5 into the new locker 6, then the 1 from locker 4 into locker 5, then the 4 from locker 3 into locker 4, then the 9 from locker 2 into locker 3. Only then is locker 2 free for the 5.

Inserting a 5 at locker 2: the values after it shift one door right, starting from the end

Here is the insert written out by hand, counting each move:

lockers = [7, 3, 9, 4, 1, 8]
print(lockers[4])                 # 1
def insert(a, i, x):
    a.append(None)                # one more door
    moves = 0
    for k in range(len(a) - 1, i, -1):
        a[k] = a[k - 1]           # shift right
        moves += 1
    a[i] = x
    return moves
print(insert(lockers, 2, 5))      # 4
print(lockers)        # [7, 3, 5, 9, 4, 1, 8]

Output:

1
4
[7, 3, 5, 9, 4, 1, 8]

Four moves for six lockers. With a million lockers, an insert near the front shifts nearly all of them: up to a million moves. The work grows in step with the number of items, which is O(n).

The 5 in locker 2 after 4 moves; with a million lockers, up to a million moves, O(n)

Python's own lockers.insert(2, 5) gives the same list. It does the shifting in C, so it's much faster per move, but it still moves every slot after the insertion point. The cost is still O(n).

What does this mean for your code?

Operation What happens Cost
a[i] compute start + i × size, jump O(1)
a.append(x) write into the next free slot at the end O(1) on average
a.insert(i, x) shift everything after i one slot right O(n)
a.pop(0) shift everything left by one O(n)

Side by side is what makes reading instant and inserting slow. If your program mostly reads by position, an array is ideal. If it keeps adding and removing at the front, a structure built for that, like collections.deque, avoids the shifting.

Try it

Insert a new item at the very front of a list of ten lockers. Using the same insert function, how many moves does it take? And how many for a list of a million?