Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

NumPy’s repeat() copies each element of an array in place, so every value is followed by its own copies. Whether you get repeated elements, repeated rows or repeated columns depends entirely on the axis argument. Leave axis out and the array is flattened first. Set it to 0 and whole rows are repeated; set it to 1 and values are repeated within each row. This guide walks through each case, shows the output shape you should expect, and explains why tile() produces a different result from the same counts.

The signature and what each argument does

The NumPy 2.5 reference documents the function as numpy.repeat(a, repeats, axis=None). The three arguments work as follows:

  • a is the input. Any array-like value is accepted.
  • repeats is either a single integer, applied to every element, or an array of integers. An array of counts is broadcast to match the length of the selected axis, so it must line up with that axis.
  • axis selects the dimension to expand. The default, None, flattens the input into one dimension before repeating.

Repeating elements of a flat or flattened array

With the default axis=None, the input is treated as a one-dimensional sequence and every value is repeated in turn. This is the case most tutorials show first, and it is easy to get wrong with 2-D data because the array silently loses its shape.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import numpy as np

np.repeat(3, 4)
# array([3, 3, 3, 3])

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

The 2-by-2 array became a flat array of eight values. If you want to keep the shape, you must pass an axis.

Repeating rows with axis=0

For a two-dimensional array with shape (rows, columns), axis=0 acts on the first dimension, so each whole row is duplicated. A scalar count of 2 turns two rows into four, and the column count stays the same.

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

To repeat rows by different amounts, pass one count per row. The NumPy reference example below repeats the first row once and the second row twice:

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

Repeating columns with axis=1

Commonly described as repeating columns, axis=1 acts on the second dimension. Each value is copied within its own row, so every row grows wider while the number of rows is unchanged. Each individual column position is duplicated horizontally, which is why the column count increases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
x = np.array([[1, 2], [3, 4]])
np.repeat(x, 3, axis=1)
# array([[1, 1, 1, 2, 2, 2],
#        [3, 3, 3, 4, 4, 4]])

Notice that the result is not [[1, 2, 1, 2, 1, 2], ...]. Repetition happens per element, not per row pattern. That distinction returns in the comparison with tile() below.

Per-position counts and how the output length is calculated

When repeats is an array, the count at each position controls how many times that element, row or column appears. The length of the repeated axis becomes the sum of the counts. For example, if a 2-row array is repeated with [1, 2] along axis=0, the result has 1 + 2 = 3 rows.

A count array must match the length of the selected axis. If you pass two counts for a three-row array, NumPy cannot broadcast them and raises an error, so check the axis length first with x.shape.

Predicting the output shape

Use these rules before running the call. Assume x has shape (2, 2) unless noted.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Call Output shape What changes
np.repeat(x, 2) (8,) Flattened; no axis kept
np.repeat(x, 2, axis=0) (4, 2) Rows doubled; columns unchanged
np.repeat(x, [1, 2], axis=0) (3, 2) Rows repeated 1 and 2 times; total is the sum of counts
np.repeat(x, 3, axis=1) (2, 6) Columns tripled within each row; rows unchanged

For a 2-D array of shape (m, n) and a scalar count k, the output is (m*k, n) with axis=0 and (m, n*k) with axis=1.

Best Value
NumPy - Python Library for Software Developers, Programmers T-Shirt
  • NumPy is perfect for data scientists and engineers using Python. NumPy powers machine learning, financial modeling, and AI development. NumPy is essential for data analysis, physics research, big data processing in tech, and science research analytics
  • NumPy offers mathematical functions, random number generators, linear algebra routines, Fourier transforms. NumPy Python library adds support for large multi-dimensional arrays and matrices, with high-level mathematical functions to operate on these arrays
  • Lightweight, Classic fit, Double-needle sleeve and bottom hem
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

repeat() versus tile(): the same counts, different results

repeat() duplicates each element where it stands. tile() repeats the entire input as a block. The clearest way to see this is to use a simple sequence:

np.repeat([1, 2], 2)   # array([1, 1, 2, 2])
np.tile([1, 2], 2)     # array([1, 2, 1, 2])

The difference widens with two-dimensional input. Here is the same 2-by-2 array passed through each function:

a = np.array([[1, 2], [3, 4]])
np.tile(a, 2)
# array([[1, 2, 1, 2],
#        [3, 4, 3, 4]])

np.tile(a, (2, 1))
# array([[1, 2],
#        [3, 4],
#        [1, 2],
#        [3, 4]])

The NumPy reference for tile() describes reps as repetition counts per dimension, given as a tuple. If the tuple has more dimensions than the input, NumPy prepends dimensions to the input. If the input has more dimensions than the tuple, NumPy prepends ones to the tuple. Both behaviours are shown in the reference examples.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Aspect numpy.repeat numpy.tile
Unit copied Each element, row or column in place The whole input pattern as a block
Count control One count per element along one axis (scalar or array) One repetition count per dimension (scalar or tuple)
Default with no options Flattens the input (axis=None) Keeps the input shape and tiles it
Example result for [1, 2] with 2 [1, 1, 2, 2] [1, 2, 1, 2]

When you need neither: use broadcasting

Many people reach for repeat() or tile() only to line up shapes for an arithmetic operation. The NumPy reference for tile() states: “Although tile may be used for broadcasting, it is strongly recommended to use numpy’s broadcasting operations and functions.” If the goal is to combine a row or column with a larger array, broadcasting usually avoids building the repeated copy at all. For example, adding a one-dimensional array of length 2 to a 2-by-2 array broadcasts across the rows without any repeat() or tile() call.

Quick decision guide

  • Need each value duplicated, with the shape flattened: np.repeat(a, k).
  • Need whole rows duplicated: np.repeat(a, k, axis=0).
  • Need values duplicated within each row: np.repeat(a, k, axis=1).
  • Need the whole block repeated as a pattern: np.tile(a, reps).
  • Need the shapes to match for an operation: try broadcasting first.

The behaviour described here reflects the NumPy 2.5 stable reference for numpy.repeat and numpy.tile. Reference pages can change as later versions are released, so confirm the signature in the documentation for the version installed on your system with np.__version__.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.