As a python programmer, one must be aware of why Numpy is better than Lists and how can we prove it.
See, as a beginner, you can avoid this question, but if you have to work with big data sets;
you should be aware of the benefits Numpy arrays have over lists.
In this article, we will discuss in what ways Numpy arrays are better and more suitable than lists.
Numpy Data Structure performs better in:
1) FUNCTIONALITY :
Numpy array have inbuilt functions like linear algebra operations built-in;
which makes them better than lists.
2) SIZE :
Numpy array takes up less space or size.
Let us understand with the help of an example;
- First, we will import the numpy library, time and getsizeof from the system library.
- Then, we will create an empty list and get its size.
- We will fill the list with the n range of elements; i.e. 3000000 and will again get the size.
>> we see that the size has considerably increased.
- Now, let’s convert the list to NumPy and take its size:
- To prove our point we print out the difference between both the sizes:
Hence, our point is being proved by the above example;
that NumPy takes up less space as compared to the list data structure.
3) PERFORMANCE :
They have a need for speed and are faster than lists.
Let’s understand with the help of an example:
- First of all, create a list of n elements and then print first and last 5 items:
- Modifying lists and multiplying each item by 10 and printing some of them:
>> we see here that all elements are now multiplied by 10.
- Now, let’s calculate the processing time taken for this procedure:
- Now, we will convert the list into numpy :
- Modifying the array and then printing the elements:
- And, then calculate the time for the process:
- To prove our point we will find the difference between the two times:
>> Numpy is faster than a list by 0.53125s;
this is a small amount of time, but when you work with a large dataset, there will be a huge difference.
Numpy is designed to be efficient with matrix operations, more specifically; more processing in
NumPy is vectorized.
Vectorization involves, expressing mathematical operations; such as multiplication, that we are
using as occurring on the entire element, rather than a single statement.
With Vectorization, the underline code is parallelized, such that the operations we are performing
on NumPy array can run on multiple array elements at once, rather than looping through them
one at a time.
As long as the operations you are applying does not rely on any other array element,
as in the case of matrix multiplication; then vectorization will give you awesome speedups.
Looping over Python arrays, lists and dictionaries can be slow.
As, Vectorized operations in NumPy are mapped to highly optimized C code, making them much faster than their standard python counterparts.
Hence, I will recommend you to use Numpy rather than lists in python.
Thank you for reading!
This is the link to the code being used in the blog- https://github.com/HARITIKAPANDIT/NOTEBOOK/blob/master/Numpy%20over%20list.ipynb