All courses › Intermediate Programming › NumPy array and vectorization
NumPy array and vectorization
A NumPy array looks like a list, but arithmetic operations act elementwise on the whole array without an explicit loop. This is called vectorization and is often 10–100 times faster than a Python loop over a plain list.
each element of multiplied by
elementwise addition of equal-sized arrays
Symbols
| NumPy arrays | ||
| a scalar |
Example
a = np.array([1, 2, 3])
a * 2 gives [2 4 6] — elementwise, not repetition as for a list.
list * 2 repeats a plain Python list, while array * 2 multiplies each number — do not mix them up.