Stacking & Splitting Arrays

Stacking and splitting are the NumPy operations that join multiple arrays into one and divide one array back into several — letting you assemble and reshape datasets without manual loops.

Learn Stacking & Splitting Arrays 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.

In this lesson you'll combine arrays with concatenate, vstack, hstack, and stack, then break them apart again with split, hsplit, and vsplit, all with clear before-and-after output.

np.concatenate is the most general join. You give it a list of arrays and an axis , and it glues them together along that existing axis. For 1D arrays there is only one axis, so it simply chains them end to end.

Typing axis every time gets tedious, so NumPy provides two friendly shortcuts. np.vstack stacks arrays vertically (more rows) and np.hstack stacks them horizontally (more columns).

Expected output: the 2×3 vertical stack, the length-6 horizontal stack, and the 2×2 column join [[1 3] [2 4]] .

Unlike concatenate, np.stack introduces a brand-new dimension . Stacking two length-3 1D arrays produces a 2×3 array — the inputs become rows of a fresh axis, so the result has one more dimension than the inputs.

Expected output: a (2, 3) array stacked along axis 0, and a (3, 2) array along axis 1 pairing one element from each input per row.

np.split is the inverse of concatenate. Give it an array and the number of equal pieces, and it returns a list of sub-arrays. np.hsplit and np.vsplit are the column and row shortcuts.

Replace each ___ so the program stacks two rows vertically and then splits them back apart.

Expected output: the 2×3 array and 2 . (Answers: vstack , 2 .)

❌ ValueError: all the input array dimensions ... must match

The non-joining dimension differs between the arrays (e.g. different column counts when stacking rows).

✅ Fix: make sure every dimension except the one you join along is equal. Print each .shape to compare.

❌ ValueError: array split does not result in an equal division

np.split with an integer needs the length to divide evenly.

✅ Fix: choose a divisor that fits, or pass a list of split indices instead.

Combine an IDs column with a scores column side by side, then split the table back into its two columns.

Lesson 12 complete — you can assemble arrays at will!

You now join arrays with concatenate , vstack , hstack , and stack , and break them apart with split , hsplit , and vsplit .

🚀 Up next: Copies vs Views — learn when NumPy shares memory and when it makes a real copy.

Practice quiz

What is the key difference between np.concatenate and np.stack?

  • concatenate joins along an existing axis; stack adds a new axis
  • They are identical
  • stack only works on 1D
  • concatenate adds a dimension

Answer: concatenate joins along an existing axis; stack adds a new axis. concatenate keeps the number of dimensions; stack introduces a brand-new axis.

What does np.vstack do?

  • Sorts arrays
  • Stacks arrays vertically, adding rows
  • Splits an array
  • Adds columns

Answer: Stacks arrays vertically, adding rows. vstack stacks arrays on top of each other, producing more rows.

What does np.hstack do?

  • Adds rows
  • Transposes
  • Stacks arrays horizontally, adding columns
  • Removes duplicates

Answer: Stacks arrays horizontally, adding columns. hstack lays arrays side by side, producing more columns.

Stacking two length-3 1D arrays with np.stack gives what shape?

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

Answer: (2, 3). stack adds a new axis, turning two 1D arrays into a (2, 3) array.

What does np.concatenate require of the arrays?

  • They match in every dimension except the joining axis
  • They are identical
  • They are 1D only
  • They have the same dtype only

Answer: They match in every dimension except the joining axis. Concatenation needs all non-joining dimensions to be equal, else it raises ValueError.

Which axis does np.concatenate use to stack rows on 2D arrays?

  • axis=2
  • axis=0
  • axis=1
  • axis=-1

Answer: axis=0. axis=0 joins along rows, placing arrays on top of each other.

What does np.stack([a, b], axis=1) do to two length-3 arrays?

  • Concatenates them end to end
  • Returns shape (2, 3)
  • Pairs elements into columns, giving shape (3, 2)
  • Raises an error

Answer: Pairs elements into columns, giving shape (3, 2). axis=1 pairs one element from each input per row, giving (3, 2).

Which is the inverse operation of stacking?

  • Sorting
  • Reshaping
  • Transposing
  • Splitting

Answer: Splitting. np.split (and hsplit/vsplit) divides an array back into pieces, the inverse of stacking.

Compared to its inputs, np.stack produces an array with...

  • One more dimension
  • The same number of dimensions
  • One fewer dimension
  • Always 1 dimension

Answer: One more dimension. stack adds a new axis, so the result gains one dimension.

np.vstack([a, b]) is a shortcut for which call on 2D arrays?

vstack is concatenate with axis=0, stacking arrays vertically.