Repeat each item in a list a number of times specified in another list

numpy’s repeat function gets the job done:

>>> import numpy as np
>>> x = [2, 3, 4]
>>> y = [1, 2, 3]
>>> np.repeat(x, y)
array([2, 3, 3, 4, 4, 4])

Leave a Comment