If you know Python, you might be questioning why we need NumPy arrays since we already have Python lists. After all, these Python lists function as an array that may store numerous sorts of items. This is an excellent question, and the solution lies in the way Python keeps objects in memory.
A Python object is essentially a reference to a memory region that contains all of the object’s characteristics, such as bytes and value. Although this extra information is what makes Python a dynamically typed language, it comes at a cost, which is evident when keeping a big collection of objects, such as in an array.
Python lists are just an array of pointers, each referring to a different item in the list.
Python lists are basically an array of pointers, each pointing to a location containing the element’s information.Â
This adds a significant amount of memory and calculation overhead.Â
When all of the items in the list are of the same type, most of this information is rendered irrelevant!
To get around this, we utilise NumPy arrays with only main emphasis, that is, items of the same data type.Â
This improves the array’s storage and manipulation efficiency.Â
When the array has a big number of elements, such as hundreds or millions, this difference becomes obvious.Â
You can also execute element-wise operations with NumPy arrays, which is not feasible with Python lists!