Skip to main content
v2026.11,858 entries · CC-BY 4.0

NumPy ndarray Length: len(), .shape and .size Explained

Get the length of a NumPy ndarray with len(), .shape or .size, plus the per-axis call and the len(a.shape) mix-up, with examples from the docs.

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

  1. 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.
  2. Check the shape first. a.shape returns a tuple of array dimensions, here (2, 3). The NumPy documentation shows np.array([1, 2, 3, 4]).shape returning (4,) and np.zeros((2, 3, 4)).shape returning (2, 3, 4).
  3. Get the first-axis length. len(a) returns 2. This is the same value as a.shape[0].
  4. Get the total element count. a.size returns 6. The documentation example x = np.zeros((3, 5, 2), dtype=np.complex128) gives x.size of 30.
  5. Get the length of one axis. np.size(a, axis=1) returns 3 and np.size(a, axis=0) returns 2. 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.size and a.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.size returns a standard arbitrary-precision Python integer, whereas np.prod(a.shape) may return a fixed-size np.int_, which matters only for very large arrays where overflow is possible. Prefer a.size for 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

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about NumPy ndarray Length: len(), .shape and .size Explained

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Ask CASRAI · Regulatory Radar

Research-admin question? Get an answer that links its sources.

An AI assistant specialized in research administration. Every answer links its sources to check before you act. 2 questions free, no account. $29/month after.

  • Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.
  • Every answer numbers its sources and links each one, so you can check the source yourself.