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
prices = np.array([100, 250, 90, 400])prices * 1.18 # add 18% GST to every priceprices - prices.mean() # centre each valuea = np.array([1, 2, 3])b = np.array([10, 20, 30])a + b # [11 22 33] element-wisea * 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
- 01Vectorization applies an operation to a whole array at once — no explicit loop.
- 02Arithmetic and comparison operators work element-wise across arrays.
- 03It is faster (compiled C over contiguous memory) and shorter than a Python loop.
- 04Shift your thinking from per-element loops to whole-array expressions.
