Python Practice › NumPy Programs › Program 87

Merge arrays of the same shape

EasyNumPy Programs · 87 of 100

Write a NumPy program to merge two arrays of identical shape.

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

Approach

  1. np.vstack stacks rows; np.hstack joins side by side.

Solution

Python
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.

More numpy programs

← All 100 NumPy Programs

Leave a Comment