Python:NumPy .shape

design8615833887's avatar
Published Oct 30, 2025
Contribute to Docs

The .shape attribute of a NumPy ndarray returns a tuple of integers specifying the size of the array in each dimension. It provides information about the structure and layout of the array.

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

ndarray.shape

Parameters:

This attribute does not take any parameters.

Return value:

Returns a tuple of integers representing the size of the array along each dimension.

  • For a 1D array, it returns a single value (e.g., (5,)).
  • For a 2D array, it returns two values i.e. rows and columns (e.g., (3, 4)).
  • For higher dimensions, it includes one integer per axis.

Example

The following example demonstrates how to use the .shape attribute to get the dimensions of different ndarrays:

import numpy as np
# 1D array
arr_1d = np.array([1, 2, 3, 4, 5])
print(arr_1d.shape)
# 2D array
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
print(arr_2d.shape)
# 3D array
arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr_3d.shape)

The output for the above code will be:

(5,)
(2, 3)
(2, 2, 2)

Codebyte Example

The following codebyte example shows how to use the .shape attribute and modify array dimensions:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python:NumPy on Codecademy

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours