🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to NumPy Notes
Topic #13

NumPy Array Split


Splitting NumPy Arrays

Splitting is reverse operation of Joining.

Joining merges multiple arrays into one and Splitting breaks one array into multiple.

We use array_split() for splitting arrays, we pass it the array we want to split and the number of splits.

Example

  import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])

  newarr =
  np.array_split(arr, 3)

print(newarr)

Note: The return value is a list containing three arrays.

If the array has less elements than required, it will adjust from the end accordingly.

Example

  import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])

  newarr =
  np.array_split(arr, 4)

print(newarr)

Note: We also have the method split() available but it will not adjust the elements when elements are less in source array for splitting like in example above, array_split() worked properly but split() would fail.


Split Into Arrays

The return value of the array_split() method is a list containing each of the split as an array.

If you split an array into 3 arrays, you can access them from the result just like any array element:

Example

  import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])

  newarr =
  np.array_split(arr, 3)

print(newarr[0])
print(newarr[1])
print(newarr[2])

Splitting 2-D Arrays

Use the same syntax when splitting 2-D arrays.

Use the array_split() method, pass in the array you want to split and the number of splits you want to do.

Example

  import numpy as np

arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9,
  10], [11, 12]])

newarr = np.array_split(arr, 3)

print(newarr)

The example above returns three 2-D arrays.

Let's look at another example, this time each element in the 2-D arrays contains 3 elements.

Example

  import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10,
  11, 12], [13, 14, 15], [16, 17, 18]])

newarr = np.array_split(arr, 3)

print(newarr)

The example above returns three 2-D arrays.

In addition, you can specify which axis you want to do the split around.

The example below also returns three 2-D arrays, but they are split along the column (axis=1).

Example

  import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10,
  11, 12], [13, 14, 15], [16, 17, 18]])

newarr = np.array_split(arr, 3, axis=1)

  print(newarr)

An alternate solution is using hsplit() opposite of hstack()

Example

  import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9],
  [10, 11, 12], [13, 14, 15], [16, 17, 18]])

newarr = np.hsplit(arr, 3)

print(newarr)

Note: Similar alternates to vstack() and dstack() are available as vsplit() and dsplit().

Want to go beyond the notes?

Join CodingNow 2.0's NumPy course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

NumPy Array Split – FAQs

Quick answers about learning NumPy Array Split in NumPy.

This free note from CodingNow 2.0 explains NumPy Array Split in NumPy — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every NumPy topic on CodingNow 2.0, including NumPy Array Split, is 100% free with no signup required.
With focused practice, most students grasp NumPy Array Split in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now