DEV Community

Cover image for The Great Showdown: Python list Vs. NumPy Array. Who wins?
Zarin Saima Roza
Zarin Saima Roza

Posted on

The Great Showdown: Python list Vs. NumPy Array. Who wins?

In Python, a list is a built-in data type used to store a collection of items. It is a mutable, ordered sequence that can contain elements of different data types, including numbers, strings, and even other lists. On the other hand, NumPy is a Python library that helps with dealing with n-dimensional arrays. It's used in hard mathematical operations. We can efficiently use it as a fundamental tool for scientific computing and data analysis in Python.

Now, in 3 different scales, we will judge which one is better to use.
The ultimate battle round begins 🥁!

**

Round1:storage efficiency

**

lis=range(1000) #a list of 1000 element

#print(lis) #range(0, 1000)
#print(sys.getsizeof(5)) #any num would give 28
#print(len(lis))#1000
print(sys.getsizeof(1)*len(lis))# 5 denotes element num

arr=np.arange(1000)#same as range func, gonna create an array
print(arr.size*arr.itemsize)
# print(arr.itemsize)

# 28000 bytes -taken by lis
# 8000 bytes -taken by arr(numpy)

#Byte size of elements varies with Operating system

  • In the above code, it's evident that numpy takes less space to store. We created a list of 1000 elements. Where each element takes 28 bytes in Python conventional list,numpy stores each element in 8 bytes. So, clearly, numpy wins!

**

Round 2:Time taken to execute

**
When the dataset is very small it doesn't really matter. But let's say you are working with a very large amount of data then who'll be ahead in the race?

In Python everything is an object. The objects of Python list are not stored in a contiguous manner. Rather it stores the location of the object. Then pointers point towards the object located in its given memory location.So it takes while to execute.

On Numpy's case it has contiguous memory location. So takes less time to execute.

Image description
Image description

Let's see it in the code-


sz=1000000

lis1=range(sz)
lis2=range(sz)

arr1=np.arange(sz)
arr2=np.arange(sz)

#python list

start=time.time()
print(time.time()-start)
res= [(i+j)for i,j in zip(lis1,lis2)]
#it will take 1st ele from lis1 and lis2 and add them togrther in ele1 of res
print("Time taken by python list",(time.time()-start)*1000) #sec


#numpy array

start=time.time()
result=arr1+arr2
print("Time taken by nparray",(time.time()-start)*1000)

we see,
Time taken by python list 113.46936225891113 mili seconds
Time taken by nparray 4.576921463012695 mili seconds

python list takes a lot more time than numpy to work. So, clearly numpy again takes a lead.

**

Round3: Conveniency

**
We are already familiar with the usage of numpy. But as numpy and List do a lot of similar jobs. In which basis numpy is convenient.

  • It's very easy to add, subtract,multiply, and divide two matrices in numpy. In python, we don't get to do these.

  • NumPy offers a rich set of mathematical functions for linear algebra, Fourier analysis, random number generation, and more. This makes it a powerful tool for scientific computing.


#numpy why convenient
arr1=np.array([1,2,3])
arr2=np.array([6,90,5])
print(arr1+arr2,arr1-arr2,arr1*arr2,arr1/arr2)

Undoubtedly, numpy aces this round too!

To conclude, when it comes to fast computation, memory efficiency and convenience numpy without a say beats Python convenient List!💪

To know more functions of numpy, Check out this ipynb file-
[(https://colab.research.google.com/drive/1-WoYuFd_iZiTZ8gcRuzrbvjx6D1Mq4Rh?usp=sharing)
]

Top comments (0)