Technology3 min read

NumPy array over Python list

Abhishek Ahlawat

By Abhishek Ahlawat โ€” Recently

Founder of StudyTonight and a MERN fullstack developer. Writes about JavaScript, web development, Linux, and tech.

Summarize this article using AI

Now, the question arises what is so special about NumPy array ?

The most important benefits of using it are :

  • It consumes less memory
  • It is fast as compared to it
  • It is convinient

Now, lets compare NumPy array with Python list :

Code 1 : Comparing memory

import numpy as np
import time 
import sys

# Creating a NumPy array with 100 elements
array = np.arange(100)
# array.itemsize : Size of one element
# array.size : length of array
print("Size of NumPy array : ", array.itemsize * array.itemsize)

# Creating a list with 100 elements
# Now I'll print the size of list
list = range(0, 10)
# Multiplying size of 1 element with length of the list
print("Size of list : ", sys.getsizeof(1)*len(list))

Output :

Size of NumPy array :  64
Size of list :  280

It clearly indicates that NumPy array consumes less memory as compared to the Python list

Code 2 : Fast Computation of NumPy array

import numpy as np
import time
import sys

# let's declare the size
Size = 100000

# Creating two lists
list1 = range(Size)
list2 = range(Size)

# Creating two NumPy arrays
arr1 = np.arange(Size)
arr2 = np.arange(Size)

# Calculating time for Python list
start = time.time()
result = [(x y) for x,y in zip(list1, list2)]
          
print("Time for Python List in msec : ", (time.time() - start) * 1000)

# Calculating time for NumPy array
start = time.time()
result = arr1   arr2
print("Time for NumPy array in msec : ", (time.time()- start) * 1000)

print("\nThis means NumPy array is faster than Pyhton List")

Output :

Time for Python List in msec :  11.117696762084961
Time for NumPy array in msec :  1.2216567993164062

This means NumPy array is faster than Pyhton List

Code 3 : NumPy is convenient

import numpy as np
import time

a1 = np.array([1, 2, 3])
a2 = np.array([4, 5, 6])

# To add two array you can simply do it by
print("ADD a1 and a2 elements : ", a1   a2)

# To sub two array you can simply do it by
print("SUB a1 and a2 elements : ", a1 - a2)

# To mul two array you can simply do it by
print("MUL a1 and a2 elements : ", a1 * a2)

# Calculating time for NumPy array
start = time.time()
result = arr1   arr2
print("Time for NumPy array in msec : ", (time.time()- start) * 1000)

Output :

ADD a1 and a2 elements :  [5 7 9]
SUB a1 and a2 elements :  [-3 -3 -3]
MUL a1 and a2 elements :  [ 4 10 18]

All the above 3 codes clearly validates the point that why we prefer NumPy array over Python list.



Spotted something outdated or wrong?

Liked this? See more StudyTonight posts in Google results.

Add StudyTonight as a preferred source
Next questLIVE OPSStep 2 / 3

Practice real incidents in the browser

System Design Ops โ€” put out fires, ship fixes, and stack XP. Reading done. Time to play.

Play System Design
  • +XP

    Every sim

  • Incidents

    Live ops

  • Ranks

    Climb up

Read next