Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.hsplit

Splits an array into multiple horizontal sub-arrays. It uses axis 1 for multidimensional arrays and axis 0 for one-dimensional arrays.

How it works

Use np.hsplit to divide an array horizontally into a list of sub-arrays. For arrays with at least two dimensions it splits the second axis; for one-dimensional input it splits axis 0, so the operation remains useful for both vectors and matrices.

Syntax

numpy.hsplit(ary, indices_or_sections)

Parameters

NameTypeDescription
aryarray_likeThe array to split horizontally.
indices_or_sectionsint or 1-D arrayAn integer number of equal sections, or an array of split positions along the selected axis.

Returns

A list of sub-arrays.

Key points

  • The function returns multiple arrays produced by a horizontal, column-oriented split.
  • For multidimensional arrays, it is equivalent to calling np.split with axis 1.
  • Two-dimensional and higher-dimensional inputs are partitioned along axis 1.
  • A one-dimensional input is partitioned along its only axis.

Common mistakes

  • One-dimensional arrays are split along axis 0 rather than a column axis.

Example: Split columns evenly

import numpy as np

x = np.arange(12).reshape(3, 4)
left, right = np.hsplit(x, 2)
print(left)
print(right)

Splits a 3-by-4 array into two 3-by-2 arrays by dividing its columns.

When to use it

Separate matrix columns into feature groups

Feature engineering

Divide a vector or matrix into equal horizontal chunks

Batch preparation

Extract selected column ranges with explicit split positions

Structured arrays

Interview questions

QWhat does np.hsplit do?

np.hsplit(ary, indices_or_sections) returns a list of sub-arrays formed by horizontal splitting. It corresponds to np.split on axis 1 for multidimensional input, while one-dimensional input uses axis 0.

Follow-up: How would you split at particular column boundaries? Pass an array-like sequence of split indices instead of an integer section count.

QWhich axis does np.hsplit use?

For a two-dimensional array, horizontal splitting operates on the second axis, which is the columns axis. The rows remain in each result, while columns are distributed among the returned arrays.

Follow-up: What changes for a three-dimensional array? The same second-axis rule applies, so each result retains the other axes and receives a portion of axis 1.

QHow can the split boundaries be specified?

The second argument can be an integer or split positions. An integer requests that many equal sections; positions specify boundaries along the selected axis and can produce differently sized pieces.

Follow-up: What should be checked before using an integer? Verify that the axis length is divisible by the requested number of sections.

  • numpy.split
  • numpy.vsplit
  • numpy.array_split

Source: numpy.hsplit — NumPy v2.5 Manual. Checked 2026-10-15.