NumPy and Python Lists: Which one is better?

numpy Jun 24, 2019

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...

From the last two blogs, we have been discussing about NumPy Package, methods of creating arrays with NumPy and how to perform certain operations on arrays with NumPy. But we haven't discussed about why should we use NumPy when we have inbuilt Python Lists to work with...

The answer is simple. NumPy Package is better than Python Lists in the following ways:

  1. The arrays created with NumPy Package are more compact than Python Lists.
  2. Accessing of arrays and Reading and Writing elements to arrays is comparatively faster than doing the same in Python Lists.
  3. The arrays created in NumPy are more efficient and convenient to use.
  4. The NumPy Package provides more functionality than the inbuilt Python List.
  5. Many vector and matrix operations are available in NumPy for free.
  6. 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:

import numpy as np
import sys as s

#Creating a List with 1000 elements
list_array = range(1000)
#Calculating the size of the list
list_size = s.getsizeof(list_array) * len(list_array)
#Printing the size of the list
print("The size of python list created is: ", list_size)

#Creating a NumPy array with 1000 elements
numpy_array = np.arange(1000)
#Calculating the size of the array
numpy_size = numpy_array.size * numpy_array.itemsize
#Printing the size of the array
print("The size of numpy array created is: ", numpy_size)


Output:
The size of python list created is: 48000
The size of numpy array created is: 4000 
Testing size of Python Lists and NumPy Arrays

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:

import numpy as np
import time as t

#Defining the size of the array and list
size = 1000000

#Creating lists
list_1 = range(size)
list_2 = range(size)

#Creating arrays
numpy_arr_1 = np.arange(size)
numpy_arr_2 = np.arange(size)

#Calculating time taken to perform addition on two Python Lists
begin = t.time()
result = [ (a, b) for a, b in zip(list_1, list_2)]
time_taken = (t.time() - begin) * 1000
print("Time taken for lists: ", time_taken)

#Calculating time taken to perform addition on two NumPy Arrays
begin = t.time()
result = numpy_arr_1 + numpy_arr_2
time_taken = (t.time() - begin) * 1000
print("Time taken by NumPy: ", time_taken)


Output:
Time taken for lists: 144.99616622924805
Time taken for numpy: 53.941965103149414
Testing time taken for addition for Python Lists and NumPy Arrays

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 is 53.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:

import numpy as np
#Creating array in NumPy
arr = np.array([[1, 2, 3, 4, 5], [9, 8, 7, 6, 5]])

#Printing the dimensions of the array
print("Dimensions of the array: ", arr.ndim)

#Printing the size of each element
print("Size occupied by each element: ", arr.itemsize)

#Printing the datatype of the array
print("Datatype of the array: ", arr.dtype)

#Printing the number of elements in array
print("Number of elements in array: ", arr.size)

#Printing the shape of the array
print("Shape of the array: ", arr.shape)


Output:
Dimensions of the array: 2
Size occupied by each element: 4
Datatype of the array: int32
Number of elements in array: 10
Shape of the array: (2, 5)
Testing for convenient use of NumPy Array

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:

import numpy as np
#Create a NumPy Array
arr = np.array([[45, 99, 78, 76, 55, 40, 100, 90, 87]])

#Printing the maximum value in the array
print("Maximum value: ", arr.max())

#Printing the minimum value in the array
print("Minimum value: ", arr.min())

#Printing the median of the array elements
print("Median of the array elements: ", np.median(arr))

#Printing the sum of all the elements
print("Sum of the elements: ", arr.sum())


Output:
Maximum value:  100
Minimum value:  40
Median of the array elements:  78.0
Sum of the elements:  670
Learning to use some of the mathematical functions

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:

import numpy as np

#Creating two NumPy 2D arrays
arr1 = np.array([[33, 78, 34, 24], [90, 34, 55, 54]])
arr2 = np.array([[56, 78, 83, 89], [90, 99, 71, 14]])

#Performing addition on the arrays
arr_add = arr1 + arr2
print("Addition of the arrays: \n", arr_add)

#Performing multiplication on the arrays
arr_mul = arr1 * arr2
print("Multiplication of the arrays: \n", arr_mul)

#Performing subtraction on the arrays
arr_sub = arr1 - arr2
print("Subtraction of the arrays: \n", arr_sub)


Output:
Addition of the arrays:
[[ 89 156 117 113]
 [180 133 126  68]]

Multiplication of the arrays:
[[1848 6084 2822 2136]
 [8100 3366 3905  756]]
 
Subtraction on the arrays:
[[-23   0 -49 -65]
 [  0 -65 -16  40]]
Performing matrix operations on the 2D arrays

You can check out more Matrix Operations here on SciPy Docs.

Aditya Kumar

A thinker, reader, developer and coffee maniac. I am an undergraduate student at UPES University, Dehradun.