🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

ndarray.shape

AI & DATA SCIENCE // ndarray-shape

ndarray.shape is a tuple describing the size of an array along each of its dimensions (axes).

Syntax

arr.shape

Deep Dive Course

For a 1D array of 5 elements, shape is (5,); for a 2D array with 3 rows and 4 columns, shape is (3, 4); the length of the shape tuple itself equals the number of dimensions. shape is a read-write attribute — assigning a new, compatible tuple to it reshapes the array in place without copying data, as long as the total number of elements stays the same, which is a lower-level alternative to calling np.reshape().

1Understanding ndarray.shape

For a 1D array of 5 elements, shape is (5,); for a 2D array with 3 rows and 4 columns, shape is (3, 4); the length of the shape tuple itself equals the number of dimensions. shape is a read-write attribute — assigning a new, compatible tuple to it reshapes the array in place without copying data, as long as the total number of elements stays the same, which is a lower-level alternative to calling np.reshape().

💡

The trailing comma in a 1D shape like (5,) is not a typo — it's how Python distinguishes a one-element tuple from a plain integer in parentheses, and it's a common point of confusion for people new to NumPy.

editor.html
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape)
localhost:3000

2Practical Example

Here is a real-world application of ndarray.shape showing how it is used in production NumPy code.

editor.html
import numpy as np

arr = np.arange(12)
arr.shape = (3, 4)
print(arr)
localhost:3000

3Best Practices

Follow these guidelines when working with ndarray.shape:

1. Check .shape before performing operations that depend on specific dimensions, like matrix multiplication, to catch mismatches early with a clear error

2. Prefer np.reshape(arr, new_shape) over directly assigning to arr.shape when you want a new array object rather than modifying the existing one in place

3. Use -1 as one dimension when reshaping, either via .shape or np.reshape, to let NumPy calculate that dimension automatically from the total element count

⚠️

Tip: The trailing comma in a 1D shape like (5,) is not a typo — it's how Python distinguishes a one-element tuple from a plain integer in parentheses, and it's a common point of confusion for people new to NumPy.

editor.html
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape)
Example 02Advanced Example
import numpy as np

arr = np.arange(12)
arr.shape = (3, 4)
print(arr)

Best Practices

  • Check .shape before performing operations that depend on specific dimensions, like matrix multiplication, to catch mismatches early with a clear error
  • Prefer np.reshape(arr, new_shape) over directly assigning to arr.shape when you want a new array object rather than modifying the existing one in place
  • Use -1 as one dimension when reshaping, either via .shape or np.reshape, to let NumPy calculate that dimension automatically from the total element count

Interview Question

Why does directly reassigning arr.shape work without copying the array's data, while np.reshape() sometimes needs to make a copy?

Hint: Think about what has to stay true about the underlying memory layout for a reshape to happen without copying.

Reshaping only changes how the same underlying, contiguous block of memory is interpreted, not the actual data itself, which is possible whenever the array's memory layout is compatible with the new shape, most commonly when the array is stored contiguously in the default order. Both direct shape assignment and np.reshape() try to do this in place first; np.reshape() falls back to returning a copy specifically when the requested shape isn't compatible with a simple reinterpretation of the existing memory layout, while directly assigning to .shape instead raises an error in that same situation, since it never silently makes a copy.

Exercises

MediumPractice using ndarray.shape in a real scenario.
View Solution
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape)

Frequently Asked Questions

Why does directly reassigning arr.shape work without copying the array's data, while np.reshape() sometimes needs to make a copy?

Reshaping only changes how the same underlying, contiguous block of memory is interpreted, not the actual data itself, which is possible whenever the array's memory layout is compatible with the new shape, most commonly when the array is stored contiguously in the default order. Both direct shape assignment and np.reshape() try to do this in place first; np.reshape() falls back to returning a copy specifically when the requested shape isn't compatible with a simple reinterpretation of the existing memory layout, while directly assigning to .shape instead raises an error in that same situation, since it never silently makes a copy.

Related Functions

ndarray-ndimndarray-sizenp-reshape