Python Lists vs. Numpy Arrays - What is the difference?

Numpy Links to an external site. is the core library for scientific computing in Python. It provides a high-performance multidimensional array object, and tools for working with these arrays. A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension.

The Python core library provided Lists. A list is the Python equivalent of an array, but is resizeable and can contain elements of different types.

A common beginner question is what is the real difference here. The answer is performance. Numpy data structures perform better in:

  • Size - Numpy data structures take up less space
  • Performance - they have a need for speed and are faster than lists
  • Functionality - SciPy and NumPy have optimized functions such as linear algebra operations built in.

 

Memory

The main benefits of using NumPy arrays should be smaller memory consumption and better runtime behavior. 

For Python Lists -  We can conclude from this that for every new element, we need another eight bytes for the reference to the new object. The new integer object itself consumes 28 bytes. The size of a list "lst" without the size of the elements can be calculated with:

64 + 8 * len(lst) + + len(lst) * 28

list_structure.png

 

 

NumPy takes up less space. This means that an arbitrary integer array of length "n" in numpy needs

96 + n * 8 Bytes

whereas a list of integer

array_structure.png

So the more numbers you need to store - the better you do.

 

Speed

References:

  1. Python Course - NumPy Tutorial Links to an external site.
  2. NumPy Documentation Links to an external site.Page