Splitting Arrays
The reverse of joining: split an array into pieces with np.split, np.array_split (uneven pieces allowed), np.hsplit and np.vsplit.
Splitting is the opposite of concatenation. The coach wants to compare the first half and second half of the season, or break a squad table into top order and lower order, or cut a long array into equal batches. NumPy's split functions return a list of sub-arrays.
Definition
Splitting divides one array into a list of sub-arrays along a chosen axis.
np.splitrequires the pieces to be equal (when you ask for a number of sections);np.array_splitallows unequal pieces;np.hsplitandnp.vsplitare shortcuts for splitting column-wise and row-wise.
- List of sub-arrays — the result is a Python
list, so you can loop over it or unpack it:first, second = np.split(...). - Sections vs indices — pass an integer for "this many equal pieces", or a list of indices for "cut at these positions".
- Views — the pieces are views of the original data (no copy), so changing a piece changes the original.
Syntax
python
np.split(ary, indices_or_sections, axis=0)
np.array_split(ary, indices_or_sections, axis=0)
np.hsplit(ary, indices_or_sections) # split along axis 1 (axis 0 for 1D)
np.vsplit(ary, indices_or_sections) # split along axis 0 (needs 2D+)| Parameter | Meaning | Default |
|---|---|---|
ary | the array to split | required |
indices_or_sections | int N: N equal pieces. list like [4, 8]: cut before index 4 and before index 8 | required |
axis | axis to split along (split, array_split only) | 0 |
| Function | Unequal pieces allowed? | Axis |
|---|---|---|
np.split | ❌ (with int) — ValueError | axis argument |
np.array_split | ✅ first pieces get one extra element | axis argument |
np.hsplit | ❌ | 1 (columns); 0 for 1D arrays |
np.vsplit | ❌ | 0 (rows); input must be at least 2D |
Example — split a season into halves
python
import numpy as np
season = np.array([72, 45, 88, 31, 64, 0, 103, 52, 19, 77, 41, 93])
first, second = np.split(season, 2)
print(first)
print(second)
print(first.sum(), second.sum())text
[72 45 88 31 64 0]
[103 52 19 77 41 93]
300 385How it works: 12 values ÷ 2 sections = two pieces of 6. Unpacking the returned list into first, second is the most common pattern. The second half was stronger (385 vs 300 runs).
Example — cut at specific positions
python
print(np.split(season, [4, 8]))text
[array([72, 45, 88, 31]), array([ 64, 0, 103, 52]), array([19, 77, 41, 93])]How it works: the indices list [4, 8] means season[:4], season[4:8], season[8:] — n cut points give n + 1 pieces. The pieces do not have to be equal.
Example — uneven pieces: split vs array_split
python
np.split(season, 5)text
ValueError: array split does not result in an equal divisionpython
for part in np.array_split(season, 5):
print(part)text
[72 45 88]
[31 64 0]
[103 52]
[19 77]
[41 93]How it works: 12 values into 5 pieces: 12 = 5 × 2 + 2, so the first 2 pieces get 3 values and the rest get 2. Use array_split whenever the length might not divide evenly (e.g. batching data).
Example — hsplit and vsplit on the squad table
python
squad = np.array([[72, 45, 88, 31, 64, 0],
[56, 0, 74, 88, 32, 45],
[45, 32, 0, 67, 18, 54],
[33, 61, 45, 0, 77, 83]])
left, right = np.hsplit(squad, 2) # matches 1-3 | matches 4-6
print(left)
print(right)
top, bottom = np.vsplit(squad, 2) # top order / lower order
print(top)
print(bottom)text
[[72 45 88]
[56 0 74]
[45 32 0]
[33 61 45]]
[[31 64 0]
[88 32 45]
[67 18 54]
[ 0 77 83]]
[[72 45 88 31 64 0]
[56 0 74 88 32 45]]
[[45 32 0 67 18 54]
[33 61 45 0 77 83]]How it works: hsplit cuts vertically between columns (giving left and right parts — "horizontal" refers to the direction the pieces sit). vsplit cuts horizontally between rows (giving top and bottom). np.hsplit(a, 2) = np.split(a, 2, axis=1); np.vsplit(a, 2) = np.split(a, 2, axis=0).
Pieces are views
python
f, s = np.split(season, 2)
f[0] = 999
print(season[:3])text
[999 45 88]Changing a piece changed season. Call .copy() on a piece if you need it independent.
Joining vs splitting
| Join | Reverse (split) |
|---|---|
np.concatenate(..., axis=k) | np.split(..., axis=k) |
np.vstack | np.vsplit |
np.hstack | np.hsplit |
Common mistakes
- ❌
np.split(arr, 5)on 12 elements. ✅np.array_split(arr, 5). - ❌ Thinking
np.split(arr, [4])makes 4 pieces. ✅ A list means cut positions:[4]gives 2 pieces (arr[:4],arr[4:]). - ❌
np.vspliton a 1D array. ✅vsplitneeds at least 2 dimensions; usenp.split. - ❌ Modifying a piece and being surprised the original changed. ✅ Pieces are views —
.copy()them.
Interview tip
Q: How do you split 1,000 samples into batches of at most 64?
A: np.array_split(data, int(np.ceil(len(data) / 64))) gives 16 nearly-equal batches, or slice manually with range(0, len(data), 64) for exact 64-sized batches plus a smaller last one.
Key points to remember
- Splitting returns a Python list of sub-arrays; unpack it (first, second = np.split(a, 2)) or loop over it.
- An integer means N equal pieces; a list like [4, 8] means cut positions (N cuts → N + 1 pieces).
- np.split raises 'array split does not result in an equal division' if an integer doesn't divide evenly — use np.array_split.
- array_split gives the first (len % N) pieces one extra element.
- hsplit splits along columns (axis 1), vsplit along rows (axis 0, needs 2D+).
- The pieces are views of the original — modifying one changes the original array.
Try it yourself
Halves, cut points and uneven pieces
hsplit and vsplit the squad table
Practice: form in three phases
Revision Flashcards
Tap a card to flip it and see the answer.
