The use of arrays over lists:
- You can write vectorised code on numpy arrays, not on lists, which is convenient to read and write, and concise.
- Numpy is much faster than the standard python ways to do computations.
Vectorised code typically does not contain explicit looping and indexing etc.
(all of this happens behind the scenes, in precompiled C-code), and thus it is much more concise.
Example:2
Say you have two lists of numbers, and want to calculate the element-wise product.
The standard python list way would need you to map a lambda function (or worse - write a for
loop),
whereas with NumPy, you simply multiply the arrays.
Example:2