Why Arrays Beat Lists

One type and one block of memory buys arithmetic on the whole column.

A list of numbers is a list of objects

A Python list can hold anything, so each element is a separate object with its own type. An array holds one type in one block of memory, which is what lets numpy add a million numbers without a loop.

import numpy as np

scores = np.array([92, 88, 79])
print(scores)
print(scores.dtype)

Arithmetic applies to the whole array

Multiplying a list repeats it; multiplying an array scales every element. That difference is the reason the rest of this course exists.

import numpy as np

print([1, 2, 3] * 2)
print(np.array([1, 2, 3]) * 2)

Exercise

Try It Yourself

Turn scores into a numpy array and print every score raised by 5, using no loop.

Press Run to see output

Check Your Understanding