Repeating Arrays: tile & repeat

Reviewed & published by Brayan K

np.tile stamps a whole array end to end to build a repeating pattern, while np.repeat duplicates each individual element in place — two distinct ways to grow an array by copying its contents.

Part of the free NumPy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You'll learn when to tile versus repeat, how to control repetition along an axis of a 2D array, and how np.full gives you a constant-filled array in one clean call.

What You'll Learn in This Lesson

1 np.tile — Stamp the Whole Pattern

np.tile(arr, reps) repeats the entire array as a block. Think of it like a rubber stamp: the whole pattern is laid down again and again. With a 1D array, reps is how many times to repeat the block; with a 2D array you can pass a tuple to tile in two directions.

import numpy as np

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

np.tile(pattern, 3)        # [1 2 3 1 2 3 1 2 3]
np.tile(pattern, (2, 1))   # stack the row twice:
                           # [[1 2 3]
                           #  [1 2 3]]
import numpy as np

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

# Repeat the whole block 3 times
print(np.tile(pattern, 3))
# [1 2 3 1 2 3 1 2 3]

# Tile in 2D: 2 rows, 2 copies across each row
print(np.tile(pattern, (2, 2)))
# [[1 2 3 1 2 3]
#  [1 2 3 1 2 3]]

# ✅ Expected output:
# [1 2 3 1 2 3 1 2 3]
# [[1 2 3 1 2 3]
#  [1 2 3 1 2 3]]

2 np.repeat — Duplicate Each Element

np.repeat(arr, n) repeats each element individually, clustering its copies together. Where tile kept the pattern as a block, repeat expands every value in place. You can pass one count for all elements, or a list of counts to repeat each element a different number of times.

np.repeat([1, 2, 3], 3)        # [1 1 1 2 2 2 3 3 3]
np.repeat([1, 2, 3], [2, 0, 1]) # [1 1 3]  (per-element counts)
import numpy as np

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

# Each element repeated 3 times in place
print(np.repeat(arr, 3))
# [1 1 1 2 2 2 3 3 3]

# Per-element counts: repeat 1 twice, 2 once, 3 three times
print(np.repeat(arr, [2, 1, 3]))
# [1 1 2 3 3 3]

# Compare directly with tile on the same array
print(np.tile(arr, 3))
# [1 2 3 1 2 3 1 2 3]

# ✅ Expected output:
# [1 1 1 2 2 2 3 3 3]
# [1 1 2 3 3 3]
# [1 2 3 1 2 3 1 2 3]

3 Axes, and Constants with np.full

On a 2D array, np.repeat takes an axis: axis=0 repeats rows, axis=1 repeats columns, and leaving it off flattens first. When you only need an array of one constant value, skip tiling entirely and use np.full(shape, value) — it is the clearest tool for that job.

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

np.repeat(m, 2, axis=0)   # each row twice
np.repeat(m, 2, axis=1)   # each column twice

np.full((2, 3), 7)        # a 2x3 array of all 7s
import numpy as np

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

# Repeat each ROW twice (axis=0)
print(np.repeat(m, 2, axis=0))
# [[1 2]
#  [1 2]
#  [3 4]
#  [3 4]]

# Repeat each COLUMN twice (axis=1)
print(np.repeat(m, 2, axis=1))
# [[1 1 2 2]
#  [3 3 4 4]]

# A constant-filled array, no tiling needed
print(np.full((2, 3), 7))
# [[7 7 7]
#  [7 7 7]]

# ✅ Expected output:
# [[1 2]
#  [1 2]
#  [3 4]
#  [3 4]]
# [[1 1 2 2]
#  [3 3 4 4]]
# [[7 7 7]
#  [7 7 7]]

🎯 YOUR TURN: Fill in the Blank

You want [1 1 1 2 2 2 3 3 3] — each element duplicated in place. Pick the right function name.

import numpy as np

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

# Fill in the function so EACH element is repeated 3 times in place
result = np.___(arr, 3)

print(result)
# Expected output:
# [1 1 1 2 2 2 3 3 3]

Answer: repeat. np.tile would have given the repeating block [1 2 3 1 2 3 1 2 3] instead.

! Common Errors (And How to Fix Them)

❌ Reaching for tile when you meant repeat

Wanting [1 1 1 2 2 2] but writing tile gives a block instead:

np.tile([1, 2], 3)     # [1 2 1 2 1 2]  -- not what you wanted

✅ Fix: use repeat for per-element duplication:

np.repeat([1, 2], 3)   # [1 1 1 2 2 2]

❌ Forgetting axis on a 2D repeat

Without an axis, repeat flattens the array, so the 2D shape is lost.

✅ Fix: pass axis=0 to repeat rows or axis=1 to repeat columns.

❌ Tiling a single value to make a constant array

np.tile(7, (2, 3)) works but reads awkwardly.

✅ Fix: prefer np.full((2, 3), 7) — clearer and purpose-built.

🎯 Mini Challenge: Build a Checkerboard Header

Use tile and repeat together to build a small repeating layout, then confirm its shape.

import numpy as np

# A single 1x2 unit: dark, light
unit = np.array([0, 1])

# Repeat the row pattern 4 times across -> one striped row
row = np.tile(unit, 4)
print(row)
# [0 1 0 1 0 1 0 1]

# Stack that row 3 times down with repeat on a 2D version
grid = np.repeat(row.reshape(1, -1), 3, axis=0)
print(grid)
# [[0 1 0 1 0 1 0 1]
#  [0 1 0 1 0 1 0 1]
#  [0 1 0 1 0 1 0 1]]

print("Shape:", grid.shape)   # Shape: (3, 8)

# ✅ Expected output:
# [0 1 0 1 0 1 0 1]
# [[0 1 0 1 0 1 0 1]
#  [0 1 0 1 0 1 0 1]
#  [0 1 0 1 0 1 0 1]]
# Shape: (3, 8)

❓ Frequently Asked Questions

Lesson complete — you can grow arrays by copying!

You know that np.tile repeats the whole pattern while np.repeat duplicates each element, how to repeat along an axis, and when np.full is the cleaner choice for a constant.

Practice quiz

What does np.tile([1, 2, 3], 3) produce?

  • [1 1 1 2 2 2 3 3 3]
  • [1 2 3 1 2 3 1 2 3]
  • [3 2 1 3 2 1]
  • [1 2 3]

Answer: [1 2 3 1 2 3 1 2 3]. tile stamps the whole array end to end, repeating the block.

What does np.repeat([1, 2, 3], 3) produce?

  • [1 1 1 2 2 2 3 3 3]
  • [1 2 3 1 2 3 1 2 3]
  • [1 2 3]
  • [3 3 3 2 2 2 1 1 1]

Answer: [1 1 1 2 2 2 3 3 3]. repeat duplicates each element in place, clustering its copies.

What is the key difference between tile and repeat?

  • They are identical
  • repeat keeps the pattern as a block
  • tile repeats the whole array; repeat duplicates each element
  • tile only works on 2D arrays

Answer: tile repeats the whole array; repeat duplicates each element. tile stamps the whole pattern; repeat expands each value in place.

What does np.repeat([1, 2, 3], [2, 0, 1]) give?

  • [1 2 3]
  • [1 1 2 3]
  • [2 0 1]
  • [1 1 3]

Answer: [1 1 3]. Per-element counts: 1 twice, 2 zero times, 3 once -> [1 1 3].

On a 2D array, what does np.repeat(m, 2, axis=0) do?

  • Flattens the array
  • Repeats each row twice
  • Repeats each column twice
  • Transposes m

Answer: Repeats each row twice. axis=0 repeats along rows, duplicating each row.

What does np.repeat(m, 2, axis=1) do on a 2D array?

  • Repeats each row twice
  • Adds a new axis
  • Repeats each column twice
  • Sorts the columns

Answer: Repeats each column twice. axis=1 repeats along columns, duplicating each column.

If you call np.repeat on a 2D array with no axis, what happens?

  • It flattens first, losing the 2D shape
  • It raises an error
  • It keeps the 2D shape
  • It repeats only the first row

Answer: It flattens first, losing the 2D shape. Without axis, repeat flattens the array before repeating.

Which is the clearest way to make a 2x3 array of all 7s?

  • np.tile(7, (2, 3))
  • np.repeat(7, 6)
  • np.array(7) * 6
  • np.full((2, 3), 7)

Answer: np.full((2, 3), 7). np.full is purpose-built for a constant-filled array of a given shape.

What does np.tile([1, 2], 3) produce?

  • [1 1 1 2 2 2]
  • [1 2 1 2 1 2]
  • [1 2]
  • [2 1 2 1 2 1]

Answer: [1 2 1 2 1 2]. tile repeats the whole block: [1 2 1 2 1 2].

What shape does np.tile([1, 2, 3], (2, 2)) have?

  • (3, 2)
  • (6, 2)
  • (2, 6)
  • (2, 3)

Answer: (2, 6). reps (2, 2) tiles 2 rows down and 2 copies across -> shape (2, 6).

Continue this course

Frequently asked questions

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

np.tile copies the WHOLE array end to end, like stamping the full pattern multiple times: tile([1, 2], 3) gives [1, 2, 1, 2, 1, 2]. np.repeat duplicates EACH ELEMENT in place: repeat([1, 2], 3) gives [1, 1, 1, 2, 2, 2]. Tile preserves the original order as a repeating block; repeat clusters duplicates of each value together.

How do I control repetition along a specific axis of a 2D array?

np.repeat takes an axis argument. With axis=0 it repeats rows, with axis=1 it repeats columns, and with no axis it flattens the array first and repeats elements of the flat version. np.tile takes a reps tuple instead, where reps=(2, 3) tiles the array 2 times down and 3 times across.

When should I use np.full instead of tile or repeat?

Use np.full when you simply need an array of a given shape filled with one constant value, for example np.full((2, 3), 7). It is clearer and faster than tiling a single value. Reach for tile or repeat only when you are duplicating an existing pattern or varying counts per element.

Can np.repeat use a different count for each element?

Yes. If you pass a list of counts the same length as the array, each element is repeated its own number of times: np.repeat([1, 2, 3], [2, 0, 1]) gives [1, 1, 3] because 1 is repeated twice, 2 zero times, and 3 once. np.tile only accepts whole-array repetition counts, not per-element counts.