.resize()
Published Jun 2, 2025
Contribute to Docs
The NumPy .resize()
function returns a new array with the given shape and size. If the new size is larger than the original, the data is repeated cyclically; if smaller, the data is truncated. This function does not modify the original array.
Note: When only the shape needs to be changed without altering the size,
.reshape()
is recommended for better performance and clearer intent.
Syntax
numpy.resize(a, new_shape)
Parameters:
a
: The array to resize.new_shape
: An integer or tuple of integers defining the desired shape. A single integer creates a 1D array of that length, while a tuple defines a multi-dimensional shape.
Return value:
resized_array
: A new array of the specified shape. If the new size is larger than the original, the data is repeated cyclically. If smaller, the data is truncated. The original array remains unchanged.
Example
This example demonstrates how to resize a 1D array with 3 elements into a 2D array with 6 elements:
import numpy as np# Create a 1D array with 3 elementsarr = np.array([1, 2, 3])# Resize the 1D array to a 2D array with 2 rows and 3 columnsresized_arr = np.resize(arr, (2, 3))# Print the resultsprint("Original array:")print(arr)print("\nResized array:")print(resized_arr)
The output of this code is:
Original array:[1 2 3]Resized array:[[1 2 3][1 2 3]]
Codebyte Example
Run this code to see how .resize()
works with different sizes and shapes:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly95 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours