NumPy Splitting Arrays
split requires equal divisions when given a section count. array_split permits uneven divisions and states how an extra value is assigned.
How it works
Splitting can prepare bounded chunks or separate known windows, but a chunk boundary should not accidentally become a new statistical group. Passing explicit cut positions can represent an application-defined division. Returned pieces may share storage with the input, so copying is needed when workers or later edits require independence. Check both the number of chunks and their sizes rather than assuming every chunk has the same shape.
Run the code
Select Try in playground to preload the complete code into the NumPy editor. Nothing executes on import; choose Run when ready. The first execution loads the local Pyodide runtime and NumPy package. Each ordinary run starts fresh, with no access to this page’s cookies or personal files. In-memory streams and temporary virtual files belong to the runtime, not your PC.
For local work, use a separate virtual environment and run python splitting.py. Download the complete source. The reviewed local environment uses Python 3.12.14 and NumPy 2.5.4; the browser uses Pyodide 0.27.7 with NumPy 2.0.2. Compatible examples are checked in both. See the environment setup. No Apache or database server is required.
Example
import numpy as np
values = np.arange(7)
parts = np.array_split(values, 3)
print([part.tolist() for part in parts])
try:
np.split(values, 3)
except ValueError:
print("Rejected unequal split")
Expected result
[[0, 1, 2], [3, 4], [5, 6]] Rejected unequal split
array_split places the extra value in an earlier chunk, while split rejects the unequal partition. Those are different contracts and should be chosen according to how the receiving code handles variable lengths.
Use NumPy’s official reference for precise contracts, supported versions and numerical limitations.