Numbered slots in a row

Arrays as Bookshelves

The analogy

An array is a bookshelf with numbered slots. Because every slot is the same width and they sit in a row, you can jump straight to slot 7 without touching slots 0 to 6 — you just walk to it. But squeezing a book into the middle means shoving everything to the right.

Visualizer

Jumping straight to a slot

step 1 / 6
5
0
3
1
8
2
1
3
9
4
2
5
7
6
4
7

A shelf of numbered slots, every slot exactly the same width. That uniformity is the whole trick.8 slots

a = [12, 30, 21, 45, 9, 38, 27]

a[6]        # one multiply, one add, one read
# address = base + 6 * itemsize
# slots 0..5 are never touched — O(1)

len(a)      # also O(1), it is stored
Check yourself

Reading arr[500] on a 1000-element array costs…