This is for Python 3. I have two lists:
lista = [\'foo\', \'bar\']
listb = [2, 3]
I\'m trying to get:
newlist = [\'foo\', \'foo
You were pretty close yourself! All you needed to do was use list.extend instead of list.append
:
new_list = []
for i in zip(lista, listb):
new_list.extend([i[0]] * i[1])
this extends the list new_list
with the elements you supply (appends each individual element) instead of appending the full list.
If you need to get fancy you could always use functions from itertools
to achieve the same effect:
from itertools import chain, repeat
new_list = list(chain(*map(repeat, lista, listb)))
.extend
in a loop, though slower, beats the previous in readability.