Python Practice › NumPy Programs › Program 87
Merge arrays of the same shape
Write a NumPy program to merge two arrays of identical shape.
Output
[[1 2] [3 4] [5 6] [7 8]] [[1 2 5 6] [3 4 7 8]]
Approach
np.vstackstacks rows;np.hstackjoins side by side.
Solution
import numpy as npa = np.array([[1, 2], [3, 4]])b = np.array([[5, 6], [7, 8]])print(np.vstack((a, b)))print(np.hstack((a, b)))Output
[[1 2] [3 4] [5 6] [7 8]] [[1 2 5 6] [3 4 7 8]]
Learn the concept
New to this topic? Read the lesson, then come back.