HOWTO · NumPy

How to Get NumPy Array Length

Use len(array) for a NumPy array's first-axis length, array.size for its total element count, and array.shape for the length of each dimension.

On this page

Use len(array) to get a NumPy array’s first-axis length, array.size to count all its elements, and array.shape to inspect its dimensions. These answer different questions: for a two-dimensional array, length means the number of rows, size means the total number of entries, and shape describes both rows and columns.

Get a NumPy array’s length and total element count

Python’s built-in len() returns the length of the first axis of an array with at least one dimension. For a one-dimensional array, that is also its total element count. For a multidimensional array, it counts the outermost groups rather than every individual entry.

The ndarray.size attribute returns the total number of elements across all axes. Read it as array.size, without parentheses. It works for one-dimensional and multidimensional arrays; you do not need to flatten the array before counting its elements.

This self-contained example requires NumPy.

import numpy as np

vector = np.array([1, 2, 3, 4, 5, 6])
array = np.array([[1, 2, 3], [4, 5, 6]])

print("Vector length:", len(vector))
print("Vector element count:", vector.size)
print("Array length:", len(array))
print("Array element count:", array.size)
Vector length: 6
Vector element count: 6
Array length: 2
Array element count: 6

The vector has six entries along its single axis, so its length and total count agree. The two-dimensional array has two rows containing three entries each. Its first-axis length therefore describes the rows, while its total count includes the entries in both rows.

NumPy also provides the function np.size(array), which returns the total count when no axis is specified. Unlike the attribute, the function accepts an axis: np.size(array, axis=1) gives the length of the second axis. Choose the attribute for a total count and an explicit axis when you need a particular dimension. Neither operation counts only distinct or nonzero values.

Inspect a NumPy array’s dimensions

The ndarray.shape attribute returns a tuple with one length per axis. Read it as array.shape, without parentheses. The corresponding function is np.shape(array); it also returns the shape rather than a scalar element count.

For the two-dimensional array above, the shape is (2, 3): two rows and three columns. Access array.shape[0] for the row count and array.shape[1] for the column count. Only unpack with rows, columns = array.shape when the array has exactly two dimensions.

For a one-dimensional array of six elements, the shape is (6,), a one-item tuple, rather than the integer 6. A shape such as (2, 3, 4) describes three axes. Multiply all axis lengths to obtain the total element count; multiplying only the first two does not generalize to higher dimensions. Use array.size when you simply need that total.

The number of entries in the shape tuple is the number of dimensions, also available as array.ndim. A scalar array such as np.array(7) has shape () and one element, but no first axis, so len() is not defined for it. When an unfamiliar array does not behave like a table, inspect its shape before indexing or unpacking dimensions.