Written and maintained by CASRAI Editorial Board
Last updated
The length of a NumPy ndarray depends on which question you are asking. len(a) returns the size of the first axis, a.size returns the total number of elements, and a.shape returns the length along every axis. For a one-dimensional array all three agree; for anything with two or more axes they do not, and that gap is the source of most “wrong length” bugs.
len(), .shape and .size: which one do you need?
Use this table to pick the call that matches your question. The examples come from the NumPy reference pages for numpy.ndarray.size, numpy.size and numpy.shape, using a = np.array([[1, 2, 3], [4, 5, 6]]) (2 rows, 3 columns).
| Your question | Call | Result for the 2 x 3 array |
|---|---|---|
| How many items along the first axis (rows of a 2-D array)? | len(a) or a.shape[0] |
2 |
| How many elements in total? | a.size or np.size(a) |
6 |
| How long is a specific axis? | np.size(a, axis=1) or a.shape[1] |
3 |
| What is the length of every axis? | a.shape |
(2, 3) |
| How many axes (dimensions)? | a.ndim |
2 |
The NumPy documentation states the relationship between the first two directly: len(a) is equivalent to np.shape(a)[0] for N-dimensional arrays with N ≥ 1, and ndarray.size is the number of elements in the array, equal to np.prod(a.shape).
Common mistake: len(a.shape) is the number of dimensions
a.shape is a tuple, so len(a.shape) measures the tuple, which is the number of axes. a.ndim is the number of dimensions, which is the length of the a.shape tuple. For the 2 x 3 array above, len(a.shape) is 2 (the same as a.ndim), not 6 and not 3. Likewise, len() of a 2-D array is its row count, not its element count; if you want every element, use .size.
What it is used for in research
Array length checks are the quickest sanity test in analysis code: confirming that the number of samples matches the number of labels, that a measurement matrix has the expected number of rows and columns after loading, or that a filter did not silently drop observations. Checking a.shape before and after a transformation catches most alignment errors before they reach a statistical model. For a broader tour of shape, dimension count and dtype, see the CASRAI guide to the NumPy array object.
Install or access (official source only)
NumPy’s official install page lists pip install numpy for the Python Package Index and conda install numpy for conda environments, and recommends working inside a virtual environment. Then import it in the usual way:
import numpy as np
Step-by-step: get the length of an ndarray
- Create or load the array. For example,
a = np.array([[1, 2, 3], [4, 5, 6]]). To build a placeholder array of a given shape, see the guide to numpy.zeros. - Check the shape first.
a.shapereturns a tuple of array dimensions, here(2, 3). The NumPy documentation showsnp.array([1, 2, 3, 4]).shapereturning(4,)andnp.zeros((2, 3, 4)).shapereturning(2, 3, 4). - Get the first-axis length.
len(a)returns2. This is the same value asa.shape[0]. - Get the total element count.
a.sizereturns6. The documentation examplex = np.zeros((3, 5, 2), dtype=np.complex128)givesx.sizeof30. - Get the length of one axis.
np.size(a, axis=1)returns3andnp.size(a, axis=0)returns2. Equivalent:a.shape[1].
Length along a specific axis
numpy.size(a, axis=None) takes an optional axis. With the default None it returns the total number of elements; with an integer it returns the length of that axis. The documentation notes the function was “changed in version 2.4” to accept multiple axes, so np.size(a, axis=(0, 1)) returns 6 for the 2 x 3 array above. On NumPy releases before 2.4, pass a single integer axis or use a.shape[k].
numpy.ndarray length of a 2-D array
For a 2-D array, “length” is ambiguous: len(a) gives rows, a.shape[1] gives columns and a.size gives their product. np.shape(np.eye(3)) returns (3, 3) and np.shape([[1, 3]]) returns (1, 2), so a single row of two values has len() of 1 but size of 2.
Output and how to read it
- A plain integer from
len(a),a.sizeanda.ndim. - A tuple from
a.shape. A one-dimensional array of four items gives(4,), with the trailing comma marking a one-element tuple. - Integer type. The NumPy documentation notes that
a.sizereturns a standard arbitrary-precision Python integer, whereasnp.prod(a.shape)may return a fixed-sizenp.int_, which matters only for very large arrays where overflow is possible. Prefera.sizefor the element count. - Scalars.
np.shape(0)returns(), an empty tuple, because a scalar has no axes.
Troubleshooting
TypeError on a zero-dimensional array
A zero-dimensional array has no first axis, so len() has nothing to count. In a local check with NumPy 2.5.1, len(np.array(5)) raised TypeError: len() of unsized object. Use .size (which is 1) or .ndim (which is 0) instead.
You expected the number of elements but got the number of rows
That is len() on an array with two or more axes. Switch to .size, or see numpy.transpose if you are really counting along the wrong axis because rows and columns are swapped.
Assigning to .shape
The NumPy documentation warns that setting arr.shape is deprecated and may be removed, and that ndarray.reshape is the preferred approach. Read the shape with .shape; change it with reshape.
Counting only some elements
size counts every element, including zeros and repeats. To count elements meeting a condition or distinct values, see numpy.where and numpy.unique.
Free alternatives and equivalents
Everything above is free: NumPy and Python are open-source. These are the interchangeable ways to ask the same question, each from the official documentation.
| Option | What it returns | Works on | Source |
|---|---|---|---|
len(a) |
Length of the first axis | ndarrays with at least one axis, and any Python sequence or collection | Python docs (built-in len); NumPy numpy.shape page |
a.shape |
Tuple of the length of every axis | ndarrays | NumPy ndarray.shape |
np.shape(a) |
Same tuple, but accepts array-likes such as nested lists | Array-likes and scalars | NumPy numpy.shape |
a.size |
Total number of elements | ndarrays | NumPy ndarray.size |
np.size(a, axis) |
Total elements, or the length of the chosen axis | Array-likes | NumPy numpy.size |
a.ndim |
Number of axes | ndarrays | NumPy ndarray.ndim |
FAQ
How do I find the length of a NumPy array?
Use len(a) for the first-axis length, a.size for the total number of elements, or a.shape for the length of every axis. For a one-dimensional array they describe the same count.
What is the difference between numpy ndarray size and length?
Length (len()) is the number of items along the first axis. Size (.size) is the number of elements across all axes, equal to np.prod(a.shape). A 3 x 5 x 2 array has length 3 and size 30.
What does numpy.ndarray length return for a 2-D array?
The number of rows. A 2 x 3 array returns 2; use a.shape[1] for the 3 columns or a.size for all 6 elements.
Is len(a.shape) the length of the array?
No. It is the number of dimensions, the same value as a.ndim.
Sources
- NumPy documentation,
numpy.ndarray.size: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.size.html (checked 2026-10-29) - NumPy documentation,
numpy.ndarray.shape: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.shape.html (checked 2026-10-29) - NumPy documentation,
numpy.shape: https://numpy.org/doc/stable/reference/generated/numpy.shape.html (checked 2026-10-29) - NumPy documentation,
numpy.size: https://numpy.org/doc/stable/reference/generated/numpy.size.html (checked 2026-10-29) - NumPy documentation,
numpy.ndarray.ndim: https://numpy.org/doc/stable/reference/generated/numpy.ndarray.ndim.html (checked 2026-10-29) - NumPy installation: https://numpy.org/install (checked 2026-10-29)
- Python documentation, built-in
len: https://docs.python.org/3/library/functions.html#len (checked 2026-10-29)








