You can use zip
with a nested list
comprehension:
values = [10.5,20.2, 50.0]
occ = [3,5,1]
result = [x for x, y in zip(values, occ) for _ in range(y)]
Alternatively, without using range
:
result = [elem for x, y in zip(values, occ) for elem in [x] * y]
Output:
[10.5, 10.5, 10.5, 20.2, 20.2, 20.2, 20.2, 20.2, 50.0]
solved How to make a new list from list of values of occurrences and value? [closed]