Skip to main content
Albero Academy

Combining Arrays

Join arrays with concatenate, vstack, hstack and stack.

Manipulation8 min

Data often arrives in pieces you assemble into one array — batches of rows, columns of features. NumPy joins arrays along an existing axis (concatenate) or along a brand-new one (stack).

concatenate

join along an axis
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6]])
np.concatenate([a, b], axis=0) # stack rows → 3×2
np.concatenate([a, a], axis=1) # widen columns → 2×4

vstack, hstack, stack

readable shorthands
np.vstack([a, b]) # vertical (rows)
np.hstack([a, a]) # horizontal (columns)
# stack adds a NEW axis — a list of 2-D arrays → one 3-D array
np.stack([img1, img2, img3]) # shape (3, H, W)

Key takeaways

  1. 01np.concatenate joins arrays along an existing axis; shapes must match except on that axis.
  2. 02vstack joins rows, hstack joins columns — readable shorthands.
  3. 03np.stack creates a NEW axis, turning a list of arrays into a higher-dimensional batch.
  4. 04Choose the axis deliberately — it decides whether you grow rows or columns.

Finished this chapter?

Mark it complete to track your progress through NumPy.