🚀 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.split()

AI & DATA SCIENCE // np-split

np.split() divides an array into multiple equally-sized sub-arrays along a given axis, or at specific index positions you provide.

Syntax

np.split(arr, indices_or_sections, axis=0)

Deep Dive Course

Passing an integer N to split() divides the array into N equal parts along the given axis, raising a ValueError if the array's size along that axis doesn't divide evenly by N. Passing a list of index positions instead splits the array at exactly those boundaries, producing sub-arrays of potentially different sizes — for example, splitting at positions [3, 7] on a 10-element array produces three pieces: elements 0-2, 3-6, and 7-9.

1Understanding np.split()

Passing an integer N to split() divides the array into N equal parts along the given axis, raising a ValueError if the array's size along that axis doesn't divide evenly by N. Passing a list of index positions instead splits the array at exactly those boundaries, producing sub-arrays of potentially different sizes — for example, splitting at positions [3, 7] on a 10-element array produces three pieces: elements 0-2, 3-6, and 7-9.

💡

If split() raises a ValueError about an array that cannot be split into equal parts, either use np.array_split() instead, which allows uneven splits, or pass explicit index positions instead of a plain integer count.

editor.html
import numpy as np

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

2Practical Example

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

editor.html
import numpy as np

arr = np.arange(10)
parts = np.split(arr, [3, 7])
print(parts)
localhost:3000

3Best Practices

Follow these guidelines when working with np.split():

1. Use np.array_split() instead of np.split() when the array's size might not divide evenly into the number of pieces you want

2. Pass explicit index positions to split() when you need specific, uneven boundaries rather than N equal pieces

3. Double check the axis parameter matches the dimension you actually intend to split along, especially for multi-dimensional arrays

⚠️

Tip: If split() raises a ValueError about an array that cannot be split into equal parts, either use np.array_split() instead, which allows uneven splits, or pass explicit index positions instead of a plain integer count.

editor.html
import numpy as np

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

Examples

Example 01Basic Usage
import numpy as np

arr = np.arange(9)
parts = np.split(arr, 3)
print(parts)
Example 02Advanced Example
import numpy as np

arr = np.arange(10)
parts = np.split(arr, [3, 7])
print(parts)

Best Practices

  • Use np.array_split() instead of np.split() when the array's size might not divide evenly into the number of pieces you want
  • Pass explicit index positions to split() when you need specific, uneven boundaries rather than N equal pieces
  • Double check the axis parameter matches the dimension you actually intend to split along, especially for multi-dimensional arrays

Interview Question

What happens if you call np.split() on a 10-element array asking for 3 equal pieces?

Hint: Think about whether 10 divides evenly by 3.

np.split() raises a ValueError, because it requires the array to divide exactly evenly into the requested number of pieces when given a plain integer count, and 10 doesn't divide evenly by 3. To split an array into a specific number of pieces that might not be equal, you'd use np.array_split() instead, which allows some pieces to be one element larger than others, or you could pass explicit index positions to np.split() to define the exact, uneven boundaries yourself.

Exercises

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

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

Frequently Asked Questions

What happens if you call np.split() on a 10-element array asking for 3 equal pieces?

np.split() raises a ValueError, because it requires the array to divide exactly evenly into the requested number of pieces when given a plain integer count, and 10 doesn't divide evenly by 3. To split an array into a specific number of pieces that might not be equal, you'd use np.array_split() instead, which allows some pieces to be one element larger than others, or you could pass explicit index positions to np.split() to define the exact, uneven boundaries yourself.

Related Functions

np-vsplitnp-hsplitnp-concatenate