NumPy array_split(): Split Arrays into Multiple Sub-Arrays

numpy.array_split(ary, indices_or_sections, axis=0) splits an array into multiple sub-arrays along a specified axis. Unlike strict splitting functions, array_split() allows sections that do not evenly divide the array length.

When the number of sections does not evenly divide the axis length, array_split() automatically adjusts sub-array sizes so that earlier sections contain one more element than later sections. For strict equal divisions, see split(). For stacking operations, see append().

ParameterDescription
aryInput array to be split into sub-arrays
indices_or_sectionsIf an integer, indicates the number of equal (or nearly equal) sections. If 1D array of integers, indicates split indices
axisAxis along which to split (default is 0)

Syntax and Parameters 🔝

numpy.array_split(ary, indices_or_sections, axis=0)

Equal Sections Splitting 🔝

When the number of sections evenly divides the array length, array_split() produces sub-arrays of equal size:

import numpy as np

my_ar = np.arange(9)
splits = np.array_split(my_ar, 3)
print(splits)

assert len(splits) == 3
assert np.array_equal(splits[0], np.array([0, 1, 2]))

Expected output

[array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])]

Unequal Sections Splitting 🔝

When N does not divide evenly into the requested number of sections, array_split() distributes the remainder across the initial sections:

my_ar = np.arange(9)
print(np.array_split(my_ar, 5))

print(np.array_split(my_ar, 4))

Expected output

[array([0, 1]), array([2, 3]), array([4, 5]), array([6, 7]), array([8])]
[array([0, 1, 2]), array([3, 4]), array([5, 6]), array([7, 8])]

Splitting 2D Arrays 🔝

By default, splitting operates along axis=0 (splitting rows):

my_data = np.array([[6, 3, 2], [7, 2, 2], [6, 2, 9]])
print(np.array_split(my_data, 3))

Expected output

[array([[6, 3, 2]]), array([[7, 2, 2]]), array([[6, 2, 9]])]

Accessing Sub-Array Elements 🔝

Because array_split() returns a list of sub-arrays, individual items can be accessed using standard indexing:

my_data = np.array([[6, 3, 2], [7, 2, 2], [6, 2, 9]])
my_ar = np.array_split(my_data, 3)

print(my_ar[1])       # [[7 2 2]]
print(my_ar[1][0])    # [7 2 2]
print(my_ar[1][0][2]) # 2

assert my_ar[1][0][2] == 2

Expected output

[[7 2 2]]
[7 2 2]
2

Float Dtype Splitting 🔝

Splitting floating-point arrays preserves precision across chunks:

my_ar = np.arange(9.)
print(np.array_split(my_ar, 4))

Expected output

[array([0., 1., 2.]), array([3., 4.]), array([5., 6.]), array([7., 8.])]

Practice in Google Colab 🔝

Test array_split() on 1D and 2D arrays, explore unequal section distribution, and run verification test suites in our interactive Google Colab companion notebook:

Interactive Practice: Colab & GitHub

Launch the notebook directly in Google Colab to run live code cells or inspect the source repository on GitHub:

View on GitHub Open In Colab

Continue exploring related array manipulation functions: array(), zeros(), append(), insert(), and reshape().




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer