NumPy Join and Split Arrays Practice Questions with Solutions

Introduction

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

11. 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])

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

print("First Array:")
print(array1)

print("\nSecond Array:")
print(array2)

print("\nJoined Array:")
print(joined_array)

Sample Output

First Array:
[10 20 30]

Second Array:
[40 50 60]

Joined Array:
[10 20 30 40 50 60]

Explanation

The np.concatenate() function combines two or more arrays along an existing axis.

Concepts Covered

  • concatenate()
  • 1D Array Joining
  • Array Combination

12. Python Program to Join Two 2D Arrays Row-Wise

Problem Statement

Write a Python program to join two 2D NumPy arrays vertically using axis=0.

Python Solution

import numpy as np

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

array2 = np.array([
    [7, 8, 9],
    [10, 11, 12]
])


joined_array = np.concatenate((array1, array2), axis=0)

print("Joined Array Row Wise:")
print(joined_array)

Sample Output

Joined Array Row Wise:

[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]

Explanation

When axis=0 is used, arrays are joined vertically by adding rows.

Concepts Covered

  • 2D Array Joining
  • axis=0
  • Vertical Stacking

13. Python Program to Join Two 2D Arrays Column-Wise

Problem Statement

Write a Python program to combine two NumPy arrays horizontally by joining columns.

Python Solution

import numpy as np

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

array2 = np.array([
    [7, 8],
    [9, 10],
    [11, 12]
])


joined_array = np.concatenate((array1, array2), axis=1)

print("Column Wise Joined Array:")
print(joined_array)

Sample Output

Column Wise Joined Array:

[[ 1  2  7  8]
 [ 3  4  9 10]
 [ 5  6 11 12]]

Explanation

Using axis=1 joins arrays horizontally by adding columns.

Concepts Covered

  • concatenate()
  • axis=1
  • Horizontal Joining

14. Python Program to Split a NumPy Array into Multiple Parts Using split()

Problem Statement

Write a Python program to split a NumPy array into three equal parts using the split() function.

Python Solution

import numpy as np

array = np.arange(1, 13)

print("Original Array:")
print(array)


split_arrays = np.split(array, 3)


print("\nSplit Arrays:")

for part in split_arrays:

    print(part)

Sample Output

Original Array:
[ 1  2  3  4  5  6  7  8  9 10 11 12]

Split Arrays:

[1 2 3 4]

[5 6 7 8]

[ 9 10 11 12]

Explanation

The np.split() function divides an array into equal sections.

Concepts Covered

  • split()
  • Array Division
  • 1D Arrays

15. Python Program to Split a 2D Array Row-Wise

Problem Statement

Write a Python program to split a 4×4 NumPy matrix into two parts row-wise.

Python Solution

import numpy as np

matrix = np.arange(1, 17).reshape(4, 4)

print("Original Matrix:")
print(matrix)


parts = np.split(matrix, 2, axis=0)


print("\nSplit Matrices:")

for part in parts:

    print(part)

Sample Output

Original Matrix:

[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [13 14 15 16]]


Split Matrices:

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

[[ 9 10 11 12]
 [13 14 15 16]]

Explanation

axis=0 divides the matrix by rows.

Concepts Covered

  • 2D Split
  • axis=0
  • Matrix Division

16. Python Program to Split a 2D Array Column-Wise

Problem Statement

Write a Python program to split a 4×4 NumPy matrix into two parts column-wise.

Python Solution

import numpy as np

matrix = np.arange(1, 17).reshape(4, 4)

print("Original Matrix:")
print(matrix)


parts = np.split(matrix, 2, axis=1)


print("\nColumn Split:")

for part in parts:

    print(part)

Sample Output

Column Split:

[[ 1  2]
 [ 5  6]
 [ 9 10]
 [13 14]]

[[ 3  4]
 [ 7  8]
 [11 12]
 [15 16]]

Explanation

axis=1 splits the array based on columns.

Concepts Covered

  • Column Splitting
  • axis=1
  • Matrix Operations

17. Python Program to Join Arrays Using Stack Functions

Problem Statement

Write a Python program to join two arrays using:

  • vstack()
  • hstack()

Python Solution

import numpy as np

array1 = np.array([1, 2, 3])

array2 = np.array([4, 5, 6])


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

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


print("Vertical Stack:")
print(vertical)


print("\nHorizontal Stack:")
print(horizontal)

Sample Output

Vertical Stack:

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


Horizontal Stack:

[1 2 3 4 5 6]

Explanation

  • vstack() joins arrays vertically.
  • hstack() joins arrays horizontally.

Concepts Covered

  • vstack()
  • hstack()
  • Array Joining

18. Python Program to Split an Array Using hsplit() and vsplit()

Problem Statement

Write a Python program to split a matrix using:

  • hsplit() for columns
  • vsplit() for rows

Python Solution

import numpy as np

matrix = np.arange(1, 13).reshape(3, 4)

print("Original Matrix:")
print(matrix)


row_split = np.vsplit(matrix, 3)

column_split = np.hsplit(matrix, 2)


print("\nVertical Split:")

for part in row_split:

    print(part)


print("\nHorizontal Split:")

for part in column_split:

    print(part)

Sample Output

Original Matrix:

[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]


Vertical Split:

[[1 2 3 4]]

[[5 6 7 8]]

[[ 9 10 11 12]]


Horizontal Split:

[[1 2]
 [5 6]
 [9 10]]

[[ 3  4]
 [ 7  8]
 [11 12]]

Explanation

  • vsplit() divides an array vertically by rows.
  • hsplit() divides an array horizontally by columns.

Concepts Covered

  • vsplit()
  • hsplit()
  • Matrix Splitting
  • Array Manipulation

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