Skip to main content
v2026.11,858 entries · CC-BY 4.0

NumPy Repeat: np.repeat() Syntax, axis and Per-Element Counts

How np.repeat works: axis, per-element repeat counts, the flatten default, and how it differs from np.tile, with outputs checked on NumPy 2.5.1.

Written and maintained by CASRAI Editorial Board

Last updated

numpy.repeat(a, repeats, axis=None) repeats each element of an array after itself: np.repeat([1, 2], 3) gives 1 1 1 2 2 2, not 1 2 1 2 1 2. The part most tutorials skip is the repeats argument, which can be an array of counts, so every element or row can be repeated a different number of times. This guide covers that, the flattening default when axis is omitted, and how np.repeat differs from np.tile. Examples were run on NumPy 2.5.1 and the signature checked against the NumPy 2.5 manual on 2026-10-29.

What np.repeat does in research code

In data preparation, np.repeat expands a short list into one entry per observation. A typical case is a vector of group labels with a vector of group sizes: each label is repeated by its size, and the result lines up row by row with a data matrix. It is also used to duplicate rows of a design matrix or to upsample a coarse series by a fixed factor.

The NumPy manual summarises it as “Repeat each element of an array after themselves”. Its three parameters are:

  • a (array_like): the input array.
  • repeats (int or array of ints): the number of repetitions for each element; it is broadcast to fit the shape of the given axis.
  • axis (int, optional): the axis along which to repeat values. By default the input is flattened and a flat output array is returned.

The result has the same shape as a, except along the given axis.

Install or access

NumPy’s official install page (numpy.org/install) gives pip install numpy. To keep a project isolated, create a virtual environment first with python -m venv my-env, activate it (source my-env/bin/activate on macOS and Linux, my-env\Scripts\activate on Windows) and then run pip install numpy. With conda: conda create -n my-env, conda activate my-env, conda install numpy. Confirm the install with import numpy as np; print(np.__version__).

Step-by-step: repeating elements, columns and rows

  1. Repeat every element of a flat array. np.repeat(3, 4) returns array([3, 3, 3, 3]).
  2. Omit axis on a 2-D array and it flattens. With x = np.array([[1, 2], [3, 4]]), np.repeat(x, 2) returns array([1, 1, 2, 2, 3, 3, 4, 4]), a flat array of 8 values.
  3. Repeat along columns with axis=1. np.repeat(x, 3, axis=1) returns array([[1, 1, 1, 2, 2, 2], [3, 3, 3, 4, 4, 4]]). Each value is repeated within its own row; the shape changes from (2, 2) to (2, 6).
  4. Repeat rows with axis=0. np.repeat(x, 2, axis=0) returns [[1, 2], [1, 2], [3, 4], [3, 4]], shape (4, 2).
  5. Give each row its own count. Pass a list: np.repeat(x, [1, 2], axis=0) returns array([[1, 2], [3, 4], [3, 4]]). The first row appears once and the second twice.

Per-element counts: labels by group size

The same idea works on a 1-D array. With g = np.array(['A', 'B', 'C']) and n = np.array([2, 3, 1]), np.repeat(g, n) returns ['A' 'A' 'B' 'B' 'B' 'C']. The counts array must have one entry per element along the axis being repeated (or be a single integer, which is broadcast).

The method form

Arrays also have a repeat method, ndarray.repeat(repeats, axis=None). The manual defines it as equivalent to the function, so x.repeat(2, axis=0) and np.repeat(x, 2, axis=0) return the same array.

Output and how to read it

Check the output shape before using the result. The shape matches the input everywhere except along axis, where the length becomes the sum of the repeat counts (or the length multiplied by a single integer). With no axis, the output is always 1-D with total length equal to the sum of all counts, which for a scalar count is the array size times that count. For the 2 x 2 array above, np.repeat(x, 2) has shape (8,) while np.repeat(x, 2, axis=0) has shape (4, 2).

np.repeat vs np.tile

Both functions duplicate data, but in a different order. The manual describes numpy.tile(A, reps) as constructing an array “by repeating A the number of times given by reps”, so it repeats the whole array, while repeat repeats each element in place. The table uses the same inputs as the manual’s examples.

Function Input Call Result
np.repeat [0, 1, 2] np.repeat(a, 2) [0, 0, 1, 1, 2, 2]
np.tile [0, 1, 2] np.tile(a, 2) [0, 1, 2, 0, 1, 2]
np.repeat [[1, 2], [3, 4]] np.repeat(b, 2, axis=0) [[1, 2], [1, 2], [3, 4], [3, 4]]
np.tile [[1, 2], [3, 4]] np.tile(b, (2, 1)) [[1, 2], [3, 4], [1, 2], [3, 4]]

Choose repeat when each observation must sit next to its own copies (for example, one row per measurement within a subject), and tile when the whole block must recur (for example, the same set of conditions cycled for each replicate). tile also accepts a tuple of repetitions per axis; if reps has more entries than the array has dimensions, the array is promoted by prepending new axes.

Troubleshooting

  • A 2-D input came back flat. This is the default: without axis, the input is flattened. Pass axis=0 for rows or axis=1 for columns.
  • ValueError: operands could not be broadcast together with shape (2,) (3,). The repeat list does not fit the axis. Running np.repeat(x, [1, 2, 3], axis=0) on a two-row array raises this on NumPy 2.5.1; give one count per row.
  • ValueError: repeats may not contain negative values. Raised on NumPy 2.5.1 by np.repeat(x, [-1, 2], axis=0). Counts must be zero or greater.
  • Repeated blocks in the wrong order. If you wanted the whole array to recur, use np.tile, not np.repeat.

Free alternatives

Option Use it when Source note
numpy.tile The whole array, not each element, should be repeated NumPy manual, numpy.tile
NumPy broadcasting The goal is arithmetic between arrays of different shapes The tile page notes that, although tile may be used for broadcasting, using NumPy’s broadcasting operations and functions is strongly recommended

Both are part of NumPy itself, so they need no extra installation. For the wider array toolkit, see the NumPy array guide, and for other single-function NumPy references, numpy.linspace, numpy.clip and numpy.random.choice. If you use repeated arrays in an analysis you intend to publish, record the library version and seed alongside the code; see computational reproducibility.

FAQ

What does np.repeat do?

It repeats each element of an array a given number of times, so each value appears next to its copies. Without an axis it returns a flat array.

How do I repeat rows or columns of a NumPy array?

Set the axis: axis=0 repeats rows and axis=1 repeats columns, for example np.repeat(x, 3, axis=1).

Can each element be repeated a different number of times?

Yes. Pass an array of integers as repeats, such as np.repeat(x, [1, 2], axis=0). The counts are broadcast to fit the axis.

What is the difference between np.repeat and np.tile?

repeat repeats each element after itself; tile repeats the whole array. np.repeat([0, 1, 2], 2) gives [0, 0, 1, 1, 2, 2] and np.tile([0, 1, 2], 2) gives [0, 1, 2, 0, 1, 2].

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about NumPy Repeat: np.repeat() Syntax, axis and Per-Element Counts

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Ask CASRAI · Regulatory Radar

Research-admin question? Get an answer that links its sources.

An AI assistant specialized in research administration. Every answer links its sources to check before you act. 2 questions free, no account. $29/month after.

  • Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.
  • Every answer numbers its sources and links each one, so you can check the source yourself.