NumPy Join and Split Arrays Practice Questions with Solutions

NumPy provides powerful functions to join and split arrays. These operations are commonly used in data analysis, machine learning, and scientific computing when combining datasets or dividing large arrays into smaller parts. In this chapter, you’ll practice the most important NumPy join and split array questions with complete solutions. NumPy Join and Split Arrays practice Questions with solutions help to understand the concepts.


1. Python Program to Join Two NumPy Arrays Using concatenate()

Problem Statement

Write a Python program to join two one-dimensional NumPy arrays using the concatenate() function.

Python Solution

import numpy as np

array1 = np.array([10, 20, 30])
array2 = np.array([40, 50, 60])

result = np.concatenate((array1, array2))

print(result)

Sample Output

[10 20 30 40 50 60]

Explanation

The concatenate() function joins two or more arrays into a single array.

Concepts Covered

  • concatenate()
  • Joining Arrays

2. Python Program to Join Two 2D Arrays Row-wise

Problem Statement

Write a Python program to join two two-dimensional arrays vertically.

Python Solution

import numpy as np

array1 = np.array([[1, 2], [3, 4]])
array2 = np.array([[5, 6], [7, 8]])

result = np.vstack((array1, array2))

print(result)

Sample Output

[[1 2]
 [3 4]
 [5 6]
 [7 8]]

Explanation

The vstack() function joins arrays vertically by adding rows.

Concepts Covered

  • vstack()
  • Vertical Join

3. Python Program to Join Two 2D Arrays Column-wise

Problem Statement

Write a Python program to join two two-dimensional arrays horizontally.

Python Solution

import numpy as np

array1 = np.array([[1, 2], [3, 4]])
array2 = np.array([[5, 6], [7, 8]])

result = np.hstack((array1, array2))

print(result)

Sample Output

[[1 2 5 6]
 [3 4 7 8]]

Explanation

The hstack() function joins arrays horizontally by adding columns.

Concepts Covered

  • hstack()
  • Horizontal Join

4. Python Program to Join Arrays Using stack()

Problem Statement

Write a Python program to join two arrays using the stack() function.

Python Solution

import numpy as np

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

result = np.stack((array1, array2))

print(result)

Sample Output

[[1 2 3]
 [4 5 6]]

Explanation

The stack() function joins arrays by creating a new axis.

Concepts Covered

  • stack()
  • New Axis

5. Python Program to Split an Array into Two Equal Parts

Problem Statement

Write a Python program to split a NumPy array into two equal parts.

Python Solution

import numpy as np

numbers = np.array([10, 20, 30, 40])

result = np.split(numbers, 2)

print(result)

Sample Output

[array([10, 20]), array([30, 40])]

Explanation

The split() function divides an array into equal-sized parts.

Concepts Covered

  • split()
  • Equal Splitting

6. Python Program to Split an Array into Three Parts

Problem Statement

Write a Python program to split an array into three equal parts.

Python Solution

import numpy as np

numbers = np.array([3, 6, 9, 12, 15, 18])

result = np.split(numbers, 3)

print(result)

Sample Output

[array([3, 6]), array([ 9, 12]), array([15, 18])]

Explanation

The number of elements must be divisible by the number of splits.

Concepts Covered

  • Multiple Splits
  • split()

7. Python Program to Split an Array Using array_split()

Problem Statement

Write a Python program to split an array into unequal parts.

Python Solution

import numpy as np

numbers = np.array([10, 20, 30, 40, 50])

result = np.array_split(numbers, 3)

print(result)

Sample Output

[array([10, 20]), array([30, 40]), array([50])]

Explanation

The array_split() function allows unequal splitting when equal division is not possible.

Concepts Covered

  • array_split()
  • Unequal Splitting

8. Python Program to Join Three NumPy Arrays

Problem Statement

Write a Python program to join three one-dimensional arrays into a single array.

Python Solution

import numpy as np

a = np.array([1, 2])
b = np.array([3, 4])
c = np.array([5, 6])

result = np.concatenate((a, b, c))

print(result)

Sample Output

[1 2 3 4 5 6]

Explanation

The concatenate() function can join more than two arrays.

Concepts Covered

  • Multiple Arrays
  • concatenate()

9. Python Program to Split a 2D Array

Problem Statement

Write a Python program to split a two-dimensional NumPy array row-wise.

Python Solution

import numpy as np

numbers = np.array([
    [1, 2],
    [3, 4],
    [5, 6],
    [7, 8]
])

result = np.vsplit(numbers, 2)

print(result)

Sample Output

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

Explanation

The vsplit() function splits a two-dimensional array vertically.

Concepts Covered

  • vsplit()
  • 2D Array Splitting

10. Python Program to Split a 2D Array Column-wise

Problem Statement

Write a Python program to split a two-dimensional array into two column groups.

Python Solution

import numpy as np

numbers = np.array([
    [10, 20, 30, 40],
    [50, 60, 70, 80]
])

result = np.hsplit(numbers, 2)

print(result)

Sample Output

[array([[10, 20],
       [50, 60]]), array([[30, 40],
       [70, 80]])]

Explanation

The hsplit() function divides a two-dimensional array horizontally into column groups.

Concepts Covered

  • hsplit()
  • Column Splitting

Chapter Summary

In this chapter, you learned how to join and split NumPy arrays using concatenate(), stack(), vstack(), hstack(), split(), array_split(), vsplit(), and hsplit(). These operations are widely used for preparing and organizing datasets in data analysis and machine learning.


Key Takeaways

  • concatenate() joins multiple arrays into one.
  • stack() joins arrays by creating a new axis.
  • vstack() combines arrays vertically.
  • hstack() combines arrays horizontally.
  • split() divides arrays into equal parts.
  • array_split() supports unequal splitting.
  • vsplit() and hsplit() are useful for splitting 2D arrays.

Frequently Asked Questions (FAQs)

1. What is the difference between concatenate() and stack()?

concatenate() joins arrays along an existing axis, while stack() creates a new axis when combining arrays.


2. When should I use vstack()?

Use vstack() when you want to join arrays vertically by adding rows.


3. What is the purpose of hstack()?

hstack() joins arrays horizontally by adding columns.


4. What is the difference between split() and array_split()?

split() requires equal-sized divisions, while array_split() can create unequal-sized divisions.


5. Can I join more than two NumPy arrays?

Yes. Functions like concatenate() and stack() can join multiple arrays.


6. What is vsplit() used for?

vsplit() divides a two-dimensional array into multiple parts row-wise.


7. Why are join and split operations important in NumPy?

Join and split operations help combine datasets, divide large datasets, and prepare data for machine learning and data analysis.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top