Skip to main content
Albero Academy

Vectorization

Operate on whole arrays at once instead of looping — the heart of NumPy.

Operations9 min

Vectorization means applying an operation to a whole array at once instead of element-by-element in a Python loop. It's the reason NumPy is fast and expressive — arithmetic operators work across entire arrays.

Element-wise arithmetic

no loops needed
prices = np.array([100, 250, 90, 400])
prices * 1.18 # add 18% GST to every price
prices - prices.mean() # centre each value
a = np.array([1, 2, 3])
b = np.array([10, 20, 30])
a + b # [11 22 33] element-wise
a * b # [10 40 90]

Why it's faster

The loop still happens — but in compiled C, over a contiguous block, with no per-element Python object overhead. You get speed andfewer lines. Comparisons vectorise too, producing the boolean masks you filter with.

Key takeaways

  1. 01Vectorization applies an operation to a whole array at once — no explicit loop.
  2. 02Arithmetic and comparison operators work element-wise across arrays.
  3. 03It is faster (compiled C over contiguous memory) and shorter than a Python loop.
  4. 04Shift your thinking from per-element loops to whole-array expressions.

Finished this chapter?

Mark it complete to track your progress through NumPy.