np.concatenate
Join two or more arrays end to end along an EXISTING axis — more rows with axis=0, more columns with axis=1 — and the shape rules that decide when it works.
Watch the lesson
1 video on CodeWithMunnaX
Your analyst stored the first half of the IPL season in one array and the second half in another. Now the coach wants the full season in one table. Or a new player joined the squad and his scores must become a new row. Gluing arrays together is called concatenation, and np.concatenate is the general tool behind all the "stack" helpers.
Definition
np.concatenatejoins a sequence of arrays end to end along an existing axis, returning a new array. All input arrays must have the same number of dimensions and the same size in every dimension except the joining axis.
- Sequence of arrays — you pass a tuple or list:
np.concatenate((a, b)), notnp.concatenate(a, b). - Existing axis — no new dimension is created. Two
(2, 3)arrays become(4, 3)or(2, 6), never(2, 2, 3)(that isnp.stack). - New array — the data is copied; the originals are unchanged.
- Same size except the joining axis — to add rows, the column counts must match; to add columns, the row counts must match.
Syntax
python
np.concatenate((a1, a2, ...), axis=0, out=None, dtype=None, casting="same_kind")| Parameter | Meaning | Default |
|---|---|---|
(a1, a2, ...) | tuple/list of arrays to join | required |
axis | axis to join along. 0 = rows (stack downwards), 1 = columns (side by side), None = flatten everything first | 0 |
out | optional existing array to write the result into | None |
dtype | force the result dtype (cannot be used together with out) | None (common type of inputs) |
casting | how strict dtype conversion is | "same_kind" |
Picture it
text
axis=0 (more ROWS) axis=1 (more COLUMNS)
[A A A] [A A A | B B B]
[A A A] [A A A | B B B]
-------
[B B B] (2,3)+(2,3) -> (2,6)
[B B B]
(2,3)+(2,3) -> (4,3)Example — 1D arrays: one long season
python
import numpy as np
a = np.array([10, 20, 30])
b = np.array([40, 50])
c = np.array([60])
print(np.concatenate([a, b, c]))text
[10 20 30 40 50 60]How it works: 1D arrays have only axis 0, so they are joined end to end. The lengths can be different (3 + 2 + 1 = 6) because the joining axis is the only axis.
Example — axis=1: first half + second half of the season
python
first_half = np.array([[72, 45, 88],
[56, 0, 74]]) # 2 players x 3 matches
second_half = np.array([[31, 64, 0],
[88, 32, 45]]) # same 2 players, next 3 matches
season = np.concatenate((first_half, second_half), axis=1)
print(season)
print(season.shape)text
[[72 45 88 31 64 0]
[56 0 74 88 32 45]]
(2, 6)How it works: axis=1 places the arrays side by side. The row counts (2 and 2) must match; the column counts are added (3 + 3 = 6).
Example — axis=0: new players join the squad
python
new_players = np.array([[45, 32, 0],
[33, 61, 45]])
squad = np.concatenate((first_half, new_players), axis=0)
print(squad)
print(squad.shape)text
[[72 45 88]
[56 0 74]
[45 32 0]
[33 61 45]]
(4, 3)How it works: axis=0 places the arrays one below the other. Column counts (3 and 3) must match; rows are added (2 + 2 = 4).
Example — axis=None flattens first
python
print(np.concatenate([first_half, second_half], axis=None))text
[72 45 88 56 0 74 31 64 0 88 32 45]Shape rules — when does it fail?
| Inputs | axis | Result | Why |
|---|---|---|---|
(2, 3) + (2, 3) | 0 | (4, 3) | columns match |
(2, 3) + (2, 3) | 1 | (2, 6) | rows match |
(2, 3) + (2, 4) | 1 | (2, 7) | rows match |
(2, 3) + (2, 4) | 0 | ❌ Error | columns 3 ≠ 4 |
(3,) + (1, 3) | 0 | ❌ Error | 1D vs 2D — different number of dimensions |
(3,) + (2,) | 0 | (5,) | 1D: only one axis |
The two error messages, exactly as NumPy prints them:
text
ValueError: all the input array dimensions except for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 3 and the array at index 1 has size 4
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 2 dimension(s)Note: Unlike arithmetic, concatenation does not broadcast. A
(3,)array is not automatically treated as(1, 3)— reshape it yourself, or usenp.vstack, which does that for you.
Dtype of the result
python
print(np.concatenate([np.array([1, 2]), np.array([2.5, 3.5])]))
print(np.concatenate([np.array([1, 2]), np.array([3, 4])], dtype=float))text
[1. 2. 2.5 3.5]
[1. 2. 3. 4.]Mixed int + float gives float (the common type). dtype= lets you choose.
Common mistakes
- ❌
np.concatenate(a, b)— the second argument is read asaxis. ✅np.concatenate((a, b))— pass one tuple/list. - ❌ Joining a 1D row onto a 2D table:
np.concatenate((squad, row)). ✅np.concatenate((squad, row.reshape(1, -1)))ornp.vstack((squad, row)). - ❌ Expecting
axis=0to put arrays side by side. ✅axis=0adds rows (down),axis=1adds columns (across). - ❌ Concatenating inside a loop to grow an array. ✅ Collect pieces in a Python list, concatenate once at the end.
Interview tip
Q: What is the difference between np.concatenate and np.stack?
A: concatenate joins along an existing axis, so the number of dimensions stays the same ((2,3)+(2,3) → (4,3)). stack creates a new axis, so dimensions go up by one ((2,3)+(2,3) → (2,2,3)) and all inputs must have exactly the same shape.
Key points to remember
- np.concatenate joins arrays along an EXISTING axis — the number of dimensions does not change.
- Pass the arrays as one tuple or list: np.concatenate((a, b), axis=...).
- axis=0 adds rows (stacks downwards); axis=1 adds columns (side by side); axis=None flattens first.
- All sizes must match except along the joining axis, and all inputs must have the same number of dimensions.
- Concatenation does not broadcast — a 1D (3,) row must be reshaped to (1, 3) before joining it to a 2D table.
- The result is a new array (a copy); the dtype is the common type of the inputs unless dtype= is given.
Try it yourself
axis=0 vs axis=1 on the squad table
When concatenate fails (and the fix)
Practice: build the full season
Revision Flashcards
Tap a card to flip it and see the answer.
