Here’s the easy trick on how to get the length of NumPy array.

To calculate the length of the array just use Numpy size method.

import numpy as np my_array = np.array([[1,2,3],[4,5,6], [7,8,9],[10,11,12]]) print(f"Length of my array equals: {my_array.size}")

As an output you get 12 as a length of the array.

Now you know the length but this info might not be enough for you. Let’s see the shape of the array.

import numpy as np my_array = np.array([[1,2,3],[4,5,6], [7,8,9],[10,11,12]]) print(f"Shape of my array: {my_array.shape}")

Shape of an array is 4 x 3 which is 12 in total. This is the output which gives you more information about the size of an array. You just need to remember that to get the length of an array you need to multiply the output of shape method.

Knowing shape of the array is only the point of view. You might be interesed what is the actuall size of the array from the storage point of view. This is how to get the info how many bytes your array consumes:

import numpy as np my_array = np.array([[1,2,3],[4,5,6], [7,8,9],[10,11,12]]) print(f"The array is using {my_array.nbytes} bytes.")

Thanks to nbytes method you know that the array is using 48 bytes.

This is how to get to the full pictures about the size, length, the shape and the number of bytes the array requires.