We know straight off that NumPy is better than Python Lists, but WHY???
If you also have the same question and you need answers to it then you are at the right place. Today we are going to discuss which is better to use between NumPy Package and Python Lists with examples and proofs...
The answer is simple. NumPy Package is better than Python Lists in the following ways:
The arrays created with NumPy Package are more compact than Python Lists.
Accessing of arrays and Reading and Writing elements to arrays is comparatively faster than doing the same in Python Lists.
The arrays created in NumPy are more efficient and convenient to use.
The NumPy Package provides more functionality than the inbuilt Python List.
Many vector and matrix operations are available in NumPy for free.
And the NumPy Package is Open Source and with all these properties it is obviously better to use NumPy over Python Lists.
Please execute and confirm the codes on your own system for a better understanding.
You can check out more operation on NumPy arrays here on SciPy Docs.
Why is NumPy Package more compact than Python Lists?
By the term compact we mean that NumPy arrays occupy less space as compared to Python Lists. Lets take a look at the code to confirm this:
We can clearly see that the NumPy array occupies less space than Python Lists. On my system the size of Python List is 48000 and the size of NumPy is 4000 which is way smaller as compared to List size. (You can confirm this on your own system). That is why we prefer to use NumPy array because it is more compact than Python Lists.
Why is Accessing of arrays faster in NumPy?
By the term faster, we mean that creating arrays and performing certain operations on arrays in NumPy takes comparatively less time than Python Lists. Lets take a look at the code to confirm this:
We can clearly see that NumPy arrays were added in much lesser time as compared to Python Lists. On my system the time taken for adding two python lists is 144.99616622924805 and the time taken to add two NumPy arrays is53.941965103149414. (You can confirm this on your own system). That is why it is better to use NumPy Arrays over Python Lists.
Why is NumPy Array more convenient to use?
By the term convenient we mean that performing basic operations list finding array dimensions, checking the variable type of array, finding the size of the arrays and finding the shape of the array is much simpler and easier in NumPy. Lets take a look at the code:
What are the mathematical functions provided by NumPy?
The NumPy Package provides several mathematical functions. Please refer to this list of all the Mathematical Functions provided by NumPy here on SciPy Community post.
Lets see some of the mathematical operations in the code:
How to perform Matrix operations on arrays in NumPy?
We can easily perform operations on Matrices using inbuilt array methods in NumPy, this decreases our efforts while creating complex programs to perform required operations on matrices. Lets see how:
You can check out more Matrix Operations here on SciPy Docs.