← Back to NumPy Course | Chapter 5: Array Manipulation | Lesson 6 of 7

split()

split() cuts one array into several smaller ones.

In this page:

  1. split()
Syntax
python
np.split(arr, sections)
np.split(arr, [index1, index2])

split()

np.split divides an array into equal parts, or at specific index positions if you pass a list. It returns a list of arrays. np.array_split allows unequal parts when the size does not divide evenly.

Note: np.split raises an error for uneven divisions; use array_split instead.

Example: split()

python
import numpy as np

a = np.arange(9)
print(np.split(a, 3))
print(np.split(a, [2, 5]))
print(np.array_split(np.arange(7), 3))

# Output:
# [array([0, 1, 2]), array([3, 4, 5]), array([6, 7, 8])]
# [array([0, 1]), array([2, 3, 4]), array([5, 6, 7, 8])]
# [array([0, 1, 2]), array([3, 4]), array([5, 6])]
Related Topics
Common Mistakes
  1. Splitting into a count that does not divide evenly
  2. Forgetting the result is a list
  3. Confusing split points with sizes
Chapter Summary
  • split returns a list of arrays
  • An int means equal parts
  • A list means split indices
  • array_split allows uneven sizes
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.