Interweaving two NumPy arrays efficiently

I like Josh’s answer. I just wanted to add a more mundane, usual, and slightly more verbose solution. I don’t know which is more efficient. I expect they will have similar performance.

import numpy as np

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

c = np.empty((a.size + b.size,), dtype=a.dtype)
c[0::2] = a
c[1::2] = b

Leave a Comment

tech