API · NumPy

Python NumPy numpy.shape() Function

Use numpy.shape() to get an array's dimensions as a tuple, or read them from its .shape attribute.

On this page

Use NumPy’s numpy.shape() function to find an array’s dimensions. It returns a tuple of integers, with each value giving the length along one dimension. For example, (5, 3) means five rows and three columns. The tuple describes the input’s current shape; tuple immutability does not determine whether the array itself can be reshaped.

Syntax of numpy.shape()

Call numpy.shape(a) with the value whose dimensions you want to inspect. The argument a is required and may be a NumPy array or another array-like value, such as a nested sequence. The function returns a tuple with one integer for each dimension, in order. A one-dimensional input has one tuple value; a two-dimensional input has row and column lengths; higher-dimensional inputs add a length for each additional axis.

Examples of numpy.shape()

The following example creates a two-dimensional NumPy array and prints the dimensions reported by numpy.shape().

import numpy as np

array = np.array([[11, 12, 5], [15, 6, 10], [10, 8, 12], [12, 15, 8], [34, 78, 90]])
print(np.shape(array))
(5, 3)

A tuple’s first value describes the first axis, its second value describes the second axis, and so on. For a two-dimensional array, those axes correspond to rows and columns. This makes the result useful when you need to confirm how data is arranged before selecting an axis or passing it to another operation.

For an array with no values along its single axis, the shape is (0,). A one-dimensional array containing [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] has shape (20,). An array with five rows and three columns has shape (5, 3), while a three-dimensional array arranged as three groups of two rows with three values per row has shape (3, 2, 3). These tuples describe axis lengths in order; they do not describe the values stored in the array. The one-item tuple comma in (20,) is Python syntax that distinguishes a tuple from a parenthesized integer.

This Python example creates an array with no values and retrieves its shape. It prints no output because it assigns the shape to a variable without calling print().

import numpy as np

empty_array = np.array([])
empty_shape = np.shape(empty_array)

The function reports the lengths it receives from the input’s dimensions. If a result differs from what you expected, check how the input was constructed and the order of its nested sequences. For example, a regular two-dimensional array’s first shape value is its row count and its second is its column count. NumPy reports the structure present in the data rather than inferring intended row or column labels. For ragged nested data, behavior can depend on the NumPy version and conversion rules, so use a regular array when you need consistent axis lengths.

Using the .shape attribute

A NumPy array also exposes its dimensions through the .shape attribute. For an array a, a.shape provides the same tuple as numpy.shape(a). Use the function when you want to pass a value to a general operation; use the attribute when you already have an array and want to read its dimensions directly. The attribute is accessed without parentheses, while numpy.shape() is a function call.

For example, a.shape reports (5, 3) for an array with five rows and three columns, just as numpy.shape(a) does. Neither form changes the array; both report its dimensions. If you reshape an array, reading either form afterward reports its updated dimensions. This distinction is useful when debugging data preparation: inspect the shape before an operation to confirm the expected number and order of axes, then use the resulting tuple to locate the dimension that needs attention. numpy.shape() is a function, whereas .shape is an attribute on the array.