Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.repeat

Repeats array elements a specified number of times. Use axis to repeat along a dimension, or omit it to flatten the input first.

How it works

np.repeat creates a new array by repeating each input element according to repeats. With axis set, repetition occurs along that dimension; when axis is omitted, the input is flattened first, and a scalar count applies uniformly while an integer array supplies per-element counts.

Syntax

numpy.repeat(a, repeats, axis=None)

Parameters

NameTypeDescription
aarray_likeInput array.
repeatsint or array of intsNumber of repetitions for each element; an integer or integer array.
axisint, optionalAxis along which to repeat values. If omitted, the input is flattened.

Returns

An array with repeated elements.

Key points

  • The function returns a new array containing consecutive copies of each input element.
  • The input can be any array-like object, not only an ndarray.
  • Use one integer for a uniform count or integer counts to control repetition individually.
  • Set axis to choose the dimension whose elements are repeated.
  • Omitting axis flattens the input and returns a one-dimensional array.

Common mistakes

  • Leaving axis as None changes the operation to the flattened input and produces a one-dimensional result.
  • An array of repeat counts must broadcast to the selected axis; mismatched shapes are not automatically interpreted as arbitrary per-element counts.

Example: Repeat rows with per-row counts

import numpy as np

x = np.array([[1, 2], [3, 4]])
print(np.repeat(x, [1, 2], axis=0))
# [[1 2]
#  [3 4]
#  [3 4]]

Repeats the first row once and the second row twice along axis 0.

When to use it

Dimension expansion

Expand rows or columns according to a uniform or per-position count.

Frequency-based data expansion

Turn grouped observations into repeated samples based on integer frequencies.

Sequence preparation

Construct repeated label or feature sequences for vectorized processing.

Interview questions

QWhat does np.repeat do?

np.repeat repeats array elements and returns the resulting array. The repeats argument can be one integer or an integer array, and axis controls the dimension along which repetition occurs.

Follow-up: What happens when axis is omitted? The input is flattened before repetition, so the result is flat regardless of the original shape.

QHow does axis affect a multidimensional input?

With axis=1, each element along every row is emitted consecutively according to the repeat count. For example, repeating a two-column array three times along axis 1 produces three adjacent copies of each column value.

Follow-up: How can different columns use different counts? Pass an integer array whose shape broadcasts to the selected axis, such as [1, 2] for two columns.

QHow are per-element repetition counts specified?

repeats may be a scalar integer for a uniform count or an integer array for varying counts. When an array is used, NumPy broadcasts it to fit the shape of the chosen axis.

Follow-up: What should be checked when counts vary by position? Ensure the counts array is broadcast-compatible with that axis; otherwise the call is invalid.

  • numpy.tile
  • numpy.concatenate
  • numpy.ndarray.flatten

Source: numpy.repeat — NumPy v2.5 Manual. Checked 2026-10-15.