Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk4 min

NumPy repeat(): Elements, Rows, Columns and How It Differs from tile

np.repeat() copies each element in place along one axis, while np.tile() repeats the whole array as a block. Here is how axis controls rows and columns, how output shapes change, and which function to choose.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

np.repeat() copies each element of an array in place along one axis. The axis argument decides whether you get repeated rows, repeated values within each row, or a flattened one-dimensional result. np.tile() works differently: it repeats the whole array as a block. If you want [1, 1, 2, 2], use repeat. If you want [1, 2, 1, 2], use tile.

The signature and the default behavior

In the stable NumPy 2.5 reference, the call is numpy.repeat(a, repeats, axis=None). The input a can be any array-like object. repeats is either a single integer applied to every position, or an array of integers with one count per position along the chosen axis.

The default is the detail that catches most people. With axis=None, NumPy flattens the input before repeating, so a two-dimensional array comes back one-dimensional:

import numpy as np

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

Pass an explicit axis whenever you want the two-dimensional structure preserved.

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

Repeating rows with axis=0

For an array of shape (rows, columns), axis 0 is the row dimension. Repeating along axis 0 duplicates entire rows, each copy placed directly after the original.

Repeating every row by the same count

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

A scalar count multiplies the number of rows by that count, and the number of columns is unchanged.

Repeating individual rows by different counts

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

When you pass a sequence, it must contain one count per row. Row 0 appears once and row 1 appears twice. A sequence whose length does not match the number of rows along that axis raises a ValueError.

Repeating columns with axis=1

Axis 1 is the column dimension. Repeating along axis 1 does not copy whole columns. It repeats each value in place within its row, so the copies sit next to each other and the number of columns grows. This is what people usually mean by “repeating columns,” but the mechanism is element-level.

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

Repeating every value across each row

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

Each value appears three times in a row before the next value begins. The shape changes from (2, 2) to (2, 6).

Repeating individual columns by different counts

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

Column 0 is kept once and column 1 is repeated three times, so the result has four columns. The same length rule applies: the sequence needs one entry per column.

Predicting the output shape

Once you know which axis you are changing, the output shape is predictable. For an input of shape (m, n):

Call Output shape What changes
np.repeat(a, k) (m*n*k,) Input is flattened; every element appears k times
np.repeat(a, k, axis=0) (m*k, n) Number of rows multiplied by k
np.repeat(a, k, axis=1) (m, n*k) Number of columns multiplied by k
np.repeat(a, counts, axis=0) (sum(counts), n) Each row repeated by its own count
np.repeat(a, counts, axis=1) (m, sum(counts)) Each column repeated by its own count

The rule for a sequence of counts is that the axis length becomes the sum of those counts. Check .shape after each call; it is the fastest way to catch an axis mistake.

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

repeat() versus tile()

The two functions differ in what they copy. repeat duplicates each element; tile duplicates the full pattern. The same 1-D input shows the difference:

Input np.repeat(a, 2) np.tile(a, 2)
[1, 2] [1, 1, 2, 2] [1, 2, 1, 2]

On a two-dimensional array, tile with a scalar repeats horizontally, and a tuple controls each dimension separately:

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

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

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

The reps tuple is aligned to the input’s dimensions. If reps has more dimensions than the input, NumPy prepends dimensions to the input. If the input has more dimensions than reps, NumPy prepends ones to reps. For example, np.tile([1, 2], (2, 2)) treats the input as shape (1, 2) and returns [[1, 2, 1, 2], [1, 2, 1, 2]].

The table below compares the two on the axes that matter when choosing:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Aspect repeat tile
Unit copied Each element, in place The whole array, as a block
Count control One scalar, or one count per position on a single axis One repetition count per dimension, via reps
Behavior without an axis argument Flattens the input Keeps the input’s dimensions
Typical result from [1, 2] with count 2 [1, 1, 2, 2] [1, 2, 1, 2]
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Which one to use

  1. Each element needs to be duplicated along one axis: use np.repeat, and set axis explicitly.
  2. The whole array pattern needs to be repeated as a block: use np.tile with a scalar or a reps tuple.
  3. The repeated data exists only to match shapes in an arithmetic operation: use broadcasting instead of building a copy. The NumPy tile reference states: “Although tile may be used for broadcasting, it is strongly recommended to use numpy’s broadcasting operations and functions.”

For example, adding a per-column offset to every row needs no copy at all:

x = np.array([[1, 2], [3, 4]])
x + np.array([10, 20])
# array([[11, 22],
#        [13, 24]])

Common mistakes

  • Omitting axis. The result is flattened, and a row or column operation that expected a two-dimensional array fails later with a shape error.
  • Mismatched count sequences. A list whose length differs from the axis length raises a ValueError. Count the rows or columns before writing the list.
  • Expecting whole-column copies from axis=1. Values repeat in place within each row. If you need [1, 2, 1, 2]-style column blocks, that is a tile result.
  • Choosing a function for speed. The NumPy reference documents what each function returns, not how fast it runs. Pick the function that produces the shape you need.

The behavior described here follows the stable NumPy 2.5 reference. If your installed version differs, confirm the signature with help(np.repeat) or the documentation for that 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.