Python:NumPy .resize()

arisdelaCruz1413618857's avatar
Published Jul 19, 2025
Contribute to Docs

The .resize() method in NumPy changes the shape of an array in-place and may alter its data if the new shape is larger, filling extra elements as needed. It’s useful when reshaping arrays for further processing or analysis.

  • 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.resize(new_shape, refcheck=True)

Parameters:

  • new_shape: The desired shape of the array. If it’s larger than the original, data is repeated to fill space.
  • refcheck: If True, checks whether the array is referenced elsewhere before resizing. Set to False to skip this safety check and force a resize (may lead to issues if other views exist).

Return value:

The .resize() method modifies the array in-place and returns nothing. If the shape is increased, values are repeated to fill the new array.

Example: Resizing a 1D Array to a Larger Shape

In this example, we resize a 1D array of length 3 to a shape of (2, 4). Since the new shape has more elements, the original values are repeated to fill the space:

import numpy as np
arr = np.array([1, 2, 3])
arr.resize((2, 4))
print(arr)

Here is the output:

[[1 2 3 0]
[0 0 0 0]]

Codebyte Example: Shrinking a 2D Array to a Smaller Shape

In this example, a 2D array is resized from shape (3, 3) to (2, 2), discarding excess elements:

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