/Curriculum/IntermediateArray Manipulation

21. NumPy Joining ArraysIntermediate

Combine arrays with concatenate(), vstack(), hstack(), stack(), and column_stack().

14 mins

Concept Overview

Joining combines multiple arrays along an existing or newly created axis. `np.vstack` stacks vertically (rows); `np.hstack` stacks horizontally (columns).

Hardware Mental Model

`vstack` is stacking plates on top of each other into a tall tower (row stack). `hstack` is pushing two tables side-by-side to make a wider table (column stack).

Key Concepts (1)Click snippet to load in editor

`np.concatenate([a, b], axis=0)` is the general function. `vstack` forces 2D vertical stacking; `hstack` joins along column dimensions.

v = np.vstack([a, b]); h = np.hstack([a, b])

⚠ Common Mistakes & Pitfalls

Avoid these frequent beginner syntax and logic traps
❌ Incorrect:np.concatenate(a, b) # Missing list/tuple wrapper
✓ Correct:np.concatenate([a, b])

Arrays to join must be passed as a single list or tuple `[a, b]` as the first argument.

Pro Tip: Always put brackets `np.vstack([a, b])` around input arrays.

📌 Quick Revision

Core takeaway points from this topic
`np.concatenate([a, b], axis=0)`: General axis concatenation.
`np.vstack([a, b])`: Stack arrays vertically (row-wise).
`np.hstack([a, b])`: Stack arrays horizontally (column-wise).
`np.stack([a, b])`: Stacks along a new dimension.
Editor: 21. NumPy Joining Arrays
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state

🎯 Try It Yourself: Practice Challenge

Hands-on Mode

Solve the objective below using NumPy vectorized syntax

Objective:Vertical Stack Two 1D Arrays

Create `a = np.array([1, 2])` and `b = np.array([3, 4])`, stack them vertically with `np.vstack([a, b])`, and print.

Expected Output Target:[[1 2] [3 4]]
Practice Workspace
Python ● Ready
Initializing Python IDE...
Ctrl+Enter
Execution Result
Click "RUN CODE" to execute and inspect array state