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
- Repeat every element of a flat array.
np.repeat(3, 4)returnsarray([3, 3, 3, 3]). - Omit axis on a 2-D array and it flattens. With
x = np.array([[1, 2], [3, 4]]),np.repeat(x, 2)returnsarray([1, 1, 2, 2, 3, 3, 4, 4]), a flat array of 8 values. - Repeat along columns with axis=1.
np.repeat(x, 3, axis=1)returnsarray([[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). - Repeat rows with axis=0.
np.repeat(x, 2, axis=0)returns[[1, 2], [1, 2], [3, 4], [3, 4]], shape (4, 2). - Give each row its own count. Pass a list:
np.repeat(x, [1, 2], axis=0)returnsarray([[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. Passaxis=0for rows oraxis=1for 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, notnp.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
- NumPy manual (v2.5), numpy.repeat, checked 2026-10-29.
- NumPy manual (v2.5), numpy.tile, checked 2026-10-29.
- NumPy manual (v2.5), numpy.ndarray.repeat, checked 2026-10-29.
- NumPy, Installing NumPy, checked 2026-10-29.








