🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.hsplit()

AI & DATA SCIENCE // np-hsplit

np.hsplit() splits an array into multiple sub-arrays horizontally — by columns for 2D+ arrays, or along the only axis for 1D arrays.

Syntax

np.hsplit(arr, indices_or_sections)

Deep Dive Course

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.

editor.html
import numpy as np

matrix = np.arange(16).reshape(4, 4)
left, right = np.hsplit(matrix, 2)
print(left)
localhost:3000

2Practical Example

Here is a real-world application of np.hsplit() showing how it is used in production NumPy code.

editor.html
import numpy as np

arr = np.arange(9)
parts = np.hsplit(arr, 3)
print(parts)
localhost:3000

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.

editor.html
import numpy as np

matrix = np.arange(16).reshape(4, 4)
left, right = np.hsplit(matrix, 2)
print(left)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

matrix = np.arange(16).reshape(4, 4)
left, right = np.hsplit(matrix, 2)
print(left)
Example 02Advanced Example
import numpy as np

arr = np.arange(9)
parts = np.hsplit(arr, 3)
print(parts)

Best Practices

  • Use hsplit() for the specific, common case of splitting a 2D array by columns, instead of the more generic np.split(arr, ..., axis=1)
  • Remember hsplit() also works on 1D arrays, unlike vsplit(), splitting them along their only axis
  • Use explicit index positions when columns need to be split unevenly, rather than an integer count requiring even division

Interview Question

Why does np.hsplit() work fine on a plain 1D array while np.vsplit() raises an error on the same input?

Hint: Think about which axis each function targets, and what exists for a 1D array.

vsplit() always splits along the first axis specifically, treating it as 'rows', a concept that requires a second axis, columns, to actually exist alongside it, so it demands at least 2 dimensions. hsplit() targets the last, or second, axis for multi-dimensional arrays, but falls back to splitting along the only axis a 1D array has when there's no second axis available, mirroring hstack()'s similar 1D special-casing. This asymmetry between the two functions' handling of 1D input is a common source of confusion.

Exercises

MediumPractice using np.hsplit() in a real scenario.
View Solution
import numpy as np

matrix = np.arange(16).reshape(4, 4)
left, right = np.hsplit(matrix, 2)
print(left)

Frequently Asked Questions

Why does np.hsplit() work fine on a plain 1D array while np.vsplit() raises an error on the same input?

vsplit() always splits along the first axis specifically, treating it as 'rows', a concept that requires a second axis, columns, to actually exist alongside it, so it demands at least 2 dimensions. hsplit() targets the last, or second, axis for multi-dimensional arrays, but falls back to splitting along the only axis a 1D array has when there's no second axis available, mirroring hstack()'s similar 1D special-casing. This asymmetry between the two functions' handling of 1D input is a common source of confusion.

Related Functions

np-vsplitnp-splitnp-hstack