by using 2 lists
word = [wood, key, apple, tree]
times = [2,1,3,4]
I want to make as a below result.
result = [wood, wood, key, apple, apple, apple, tree, tree, tree, tree]
i tried word * times it doesn't work.
You can use a list comprehension with zip:
word = ['wood', 'key', 'apple', 'tree']
times = [2,1,3,4]
result = [a for a, b in zip(word, times) for _ in range(b)]
Output:
['wood', 'wood', 'key', 'apple', 'apple', 'apple', 'tree', 'tree', 'tree', 'tree']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With