For a 2D, or higher-dimensional, array, hsplit() divides it along its second axis, columns, either into N equal groups, an integer argument, or at specific column-index boundaries, a list argument. For a 1D array specifically, since there's no second axis, hsplit() instead splits along the only axis that exists, mirroring how np.hstack() also has special-cased 1D behavior — this parallel special-casing is worth remembering alongside vstack/vsplit's stricter 2D-only requirement.
1Understanding np.hsplit()
For a 2D, or higher-dimensional, array, hsplit() divides it along its second axis, columns, either into N equal groups, an integer argument, or at specific column-index boundaries, a list argument. For a 1D array specifically, since there's no second axis, hsplit() instead splits along the only axis that exists, mirroring how np.hstack() also has special-cased 1D behavior — this parallel special-casing is worth remembering alongside vstack/vsplit's stricter 2D-only requirement.
Unlike vsplit(), hsplit() works on 1D arrays too, splitting along the only available axis — the two functions aren't perfectly symmetric in what input dimensionality they accept.
import numpy as np
matrix = np.arange(16).reshape(4, 4)
left, right = np.hsplit(matrix, 2)
print(left)2Practical Example
Here is a real-world application of np.hsplit() showing how it is used in production NumPy code.
import numpy as np
arr = np.arange(9)
parts = np.hsplit(arr, 3)
print(parts)3Best Practices
Follow these guidelines when working with np.hsplit():
1. Use hsplit() for the specific, common case of splitting a 2D array by columns, instead of the more generic np.split(arr, ..., axis=1)
2. Remember hsplit() also works on 1D arrays, unlike vsplit(), splitting them along their only axis
3. Use explicit index positions when columns need to be split unevenly, rather than an integer count requiring even division
Tip: Unlike vsplit(), hsplit() works on 1D arrays too, splitting along the only available axis — the two functions aren't perfectly symmetric in what input dimensionality they accept.
import numpy as np
matrix = np.arange(16).reshape(4, 4)
left, right = np.hsplit(matrix, 2)
print(left)