Skip to content

NumPy

Arrays Beat Lists

8 min+70 XPStep 1/10

A list of objects vs a block of numbers

A Python list is a row of pointers — each element is a full object living elsewhere in memory. A NumPy array is one contiguous slab of same-typed numbers, packed back-to-back:

  • xs = [1, 2, 3] — three pointers to three int objects
  • a = np.array([1, 2, 3]) — three int64s in a row, no boxes

That layout is why NumPy is fast: math on a contiguous block is exactly what CPUs and GPUs are built for. And watch the operator change meaning — a * 2 multiplies every element, while xs * 2 repeats the list to [1, 2, 3, 1, 2, 3]. Same symbol, different species.

analogy

A list is a shelf of individually boxed items. An array is an egg carton — fixed-size slots, all the same type, lifted as one piece.