I was looking for an answer on stack, but I couldn't find the right answer.
I've two lists:
keys = ['banana', 'orange']
values = [3, 5]
And I wanna make a new list looking such like that:
newlist = ['banana', 'banana', 'banana', 'orange',
'orange', 'orange', 'orange', 'orange']
You could use zip
to iterate over (value, key)
pairs.
out = []
for e in zip(values, keys):
out.extend(e[0] * [e[1]])
It's also possible without a for loop, using a nested list comprehensions:
[y for x in zip(values, keys) for y in x[0] * [x[1]]]