Why do we use numpy arrays in place of lists in python? What is the main difference between them?
1 Answer
Numpy arrays is a typed array, the array in memory stores a homogenous, densely packed numbers.
Python list is a heterogeneous list, the list in memory stores references to objects rather than the number themselves.
This means that Python list requires dereferencing a pointer every time the code needs to access the number. While numpy array can be processed directly by numpy vector operations, which makes these vector operations much faster than anything you can code with list.
The drawback of numpy array is that if you need to access single items in the array, numpy will need to box/unbox the number into a python numeric object, which can make it slow in certain situations; and that it can't hold heterogeneous data.
list([1, 2, 3]is a list, not an array)?