Python:NumPy .repeat()

NeemaJoju's avatar
Published Nov 4, 2025
Contribute to Docs

The .repeat() method of a NumPy ndarray returns a new array where each element is repeated a specified number of times. It can repeat all elements in a flattened array or along a particular axis in multidimensional arrays.

  • 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.repeat(repeats, axis=None)

Parameters:

  • repeats: Integer or array of integers. Specifies how many times each element should be repeated. If an array, it must match the length of the axis being repeated.
  • axis (Optional): The axis along which the values are to be repeated. If None, the array is flattened before repetition.

Return value:

A new array with repeated elements. The resulting shape depends on the repeats value and whether an axis is specified.

Example

The following example creates an ndarray, then uses .repeat() to repeat the elements:

import numpy as np
a = np.array([1])
print("a repeated:", a.repeat(3))
b = np.array([[1,2],[5,6]])
print("b repeated:", b.repeat(2))
c = np.array([[1,2],[0,-1]])
print("c repeated:", c.repeat(2,axis=0))

The above code generates the following output:

a repeated: [1 1 1]
b repeated: [1 1 2 2 5 5 6 6]
c repeated: [[ 1 2]
[ 1 2]
[ 0 -1]
[ 0 -1]]

Codebyte Example

Run the following codebyte example to understand the usage of the .repeat() method:

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