Python, from Zero, for AILesson 70 of 89

Module 8 · Numbers in bulk: NumPy, pandas and the vector under every model

NumPy arrays: why one line beats a loop by a hundred times

In this lesson

  • 9 min read
  • 7 reading sections
  • Knowledge check
  • Lesson 1 of 10 in this chapter

This chapter’s goalLoad a messy CSV into pandas, clean its missing values and types, group and join it into the table a question needs, convert it to a NumPy array of the right shape and dtype, compute cosine similarity between one vector and a hundred thousand in a single vectorised expression, plot the result to a file, and explain from memory layout why the array version runs a hundred times faster than the loop it replaced.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch1:43Not available
Full audiobookNot availableNot available
A row of pointers, and a block of numberslist of 1,000,000 floatsA row of pointers, eight bytes eachEach pointer leads to a separate float objectEach object carries a type tag and a referencecountAround 40 MB, scattered across memoryHolds anything: numbers, strings, other listsnp.array of 1,000,000 float64One contiguous block of 8,000,000 bytesNo per-element object at allOne header: float64, one million of them8 MB, in one place the CPU can streamHolds exactly one dtype, and that is thebargainFive times the memory is the smaller half of the story. The hundredfold speed difference is the typelookup, the method dispatch and the allocation that Python performs for every single element, and thatNumPy performs once for the whole array.
Five times the memory is the smaller half of the story. The hundredfold speed difference is the type lookup, the method dispatch and the allocation that Python performs for every single element, and that NumPy performs once for the whole array.

Figure 8.1

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 9Free · no account needed
Lesson steps

Two ways to hold a million numbers

Python
nums = [float(i) for i in range(1_000_000)]

A Python list is a row of pointers. Each pointer leads to a separate float object somewhere in memory, and each float object carries a type tag, a reference count and the value: 24 bytes for an 8-byte number. Squaring them means, for each element: follow the pointer, check that the object is a float, extract the value, compute, allocate a new float object, store the pointer. A million times.

Python
import numpy as np
arr = np.arange(1_000_000, dtype=np.float64)

A NumPy array is one contiguous block of raw bytes, 8 million of them here, with a single header saying “these are float64, there are a million of them”. Squaring means a compiled C loop walking that block, no type checks, no allocation per element, and the CPU’s own vector instructions doing several at a time.

Measure it:

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Stuck on something?Ask Divya, your AI tutor

How to cite: Addaly (2026) ‘NumPy arrays: why one line beats a loop by a hundred times’, in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/numpy-arrays (Accessed: 21 November 2026).