Why arrays
Last updated
Every tool in the rest of this course, pandas, the chart libraries and the statistics, is built on one thing: the NumPy array. In this lesson you'll see what an array is, how it differs from a Python list, and why that difference makes it so much faster.
By the end of this lesson you can
- Make a NumPy array from a list and do maths on all of it at once
- Explain how an array stores its numbers differently from a list
- Time a Python loop against
np.sumwith%timeit - Say why
*means something different on a list and on an array
A list against an array
In Stage 1 you kept prices in a Python list. NumPy keeps them in an array. Run the cells and compare.
import numpy as np loads NumPy under the short name np. Everyone writes it that way, so you'll see np. in every example you find.
The second cell is the point: arr * 1.12 adds 12% VAT to every price at once. With a list you need a loop, or a list comprehension like the third cell, to visit each price yourself.
The answers look different, but they're the same numbers. 50.400000000000006 is how a computer stores 45 × 1.12 (lesson 1.1 showed why decimals aren't exact). NumPy just rounds what it shows to 8 digits, so the array looks tidier.
One type, packed together
Why can an array do maths on everything at once? Because of how it keeps the numbers. Step through:
The last cell of the notebook above asked arr.dtype, the array's data type. Every number in an array has the same one. That's what lets NumPy pack them side by side and treat them as one block.
You'll see int64 on your own computer: whole numbers, 64 bits (8 bytes) each. This page says int32, because the Python in your browser is a 32-bit one, so its whole numbers are 4 bytes. Everything in this stage works the same with either; lesson 2.3 shows when the size matters.
The race
Here's the difference in speed, first as a picture:
Now for real. The notebook makes 50,000 numbers as a list and as an array, checks that a loop and np.sum agree (True), and then times each with %timeit from lesson 1.3. Run all four cells.
On most computers np.sum is 30 to 100 times faster. The exact numbers depend on the machine, so yours won't match anyone else's, but the gap is always large. The more data, the more it matters: a pandas column with a million rows is a NumPy array, and every sum, mean and filter on it runs this way.
Wrong way: doubling a list
The same symbol can mean different things for a list and for an array. Run both cells.
For a list, * 2 means "repeat the list twice": [1, 2, 1, 2]. For an array, it means "multiply every number by 2": [2, 4]. Neither is an error, so Python won't warn you if you use a list where you meant an array. When a result has twice as many items as you expected, check what kind of thing you had with type().
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- NumPy: What is NumPy?: arrays against lists, and why vectorised code is faster
- NumPy: the absolute basics for beginners: making arrays, and import numpy as np
- The Python Tutorial: Lists: what * does to a list
- Harris et al., Array programming with NumPy, Nature 585, 2020: how the array works, by the people who make NumPy