Want to keep learning?

This content is taken from the Partnership for Advanced Computing in Europe (PRACE)'s online course, Python in High Performance Computing. Join the course to learn more.

Skip to 0 minutes and 12 seconds Python lists are very flexible as one can store different types of Python objects within the same list. Also, one can add and remove items at any point in the list. However, this flexibility will come with the price as the list items will be scattered all over the computer memory and accessing to memory is slower than if the data is accessed in contiguous manner. Due to inherent dynamic nature of the Python list, the data is scattered in the computer memory even when all the items are just numbers and one would construct the whole list at once.

Skip to 0 minutes and 57 seconds NumPy array on the other hand overcomes these shortcomings in the memory performance. As both type and number of array elements is fixed at the time of creating the array NumPy knows how much memory is needed for the array and allocates large enough contiguous block. Thus, the array data can be laid out contiguously into computer memory and accessing successive array elements is much faster than in case of Python list. Contiguous memory layout allows also easier passing of NumPy arrays into high-performance libraries.

Differences between Python list and NumPy array

Python lists are very flexible, but not always optimal for numerical computations.

In this video we discuss the main differences between Python list and NumPy array which is the most important Python data structure for storing numeric data.

Share this video:

This video is from the free online course:

Python in High Performance Computing

Partnership for Advanced Computing in Europe (PRACE)