Python Lists vs NumPy Arrays

ShreyaG
2 min readMay 2, 2022

NumPy arrays outperform Python Lists in number of ways.

Here are the main differences between Python Lists and NumPy arrays.

  • NumPy arrays are faster in terms of execution. This is because in arrays continuous blocks of memory is occupied whereas in lists the elements do not occupy continuous memory.

Let’s prove this by writing below code.

Python Lists take 0.015 seconds to run this operation

Now let’s see how much time NumPy arrays take in performing the same operation.

NumPy arrays performing in much less time.

You would appreciate this difference much more when the amount of data is much higher. Just add two zeros in the range number and you’d see the difference.

  • Secondly NumPy arrays are convenient and have more capabilities.
  • Thirdly, given same type and size , arrays occupy much less memory.

See the below code where I made a list and array of same size and type. Next, I calculated the amount of memory each data structure occupied by importing sys library and using getsizeof function. I multiplied the size of one element in the list (here, I took 87 which lies between 1 to 100) and multiplied that with the length of the entire list. I applied the same approach to calculate the size of NumPy array and found that the difference was massive.

Proof that NumPy arrays occupy less memory.

Now multiply this difference(2000 bytes) with a factor of 100,000 and convert it into Kbs. How much of a difference can that make ! Imagine saving that much memory when doing , let’s say machine learning analysis.

--

--