Repeating Arrays: tile & repeat
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.
Learn Repeating Arrays: tile & repeat in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
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.
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.
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.
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.
You want [1 1 1 2 2 2 3 3 3] — each element duplicated in place. Pick the right function name.
Answer: repeat . np.tile would have given the repeating block [1 2 3 1 2 3 1 2 3] instead.
Wanting [1 1 1 2 2 2] but writing tile gives a block instead:
✅ Fix: use repeat for per-element duplication:
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.
Use tile and repeat together to build a small repeating layout, then confirm its shape.
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.
🚀 Up next: Adding Axes — reshape vectors into rows and columns with np.newaxis and expand_dims .
Practice quiz
What does np.tile([1, 2, 3], 3) produce?
tile stamps the whole array end to end, repeating the block.
What does np.repeat([1, 2, 3], 3) produce?
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?
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?
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).