This is for Python 3. I have two lists:
lista = [\'foo\', \'bar\']
listb = [2, 3]
I\'m trying to get:
newlist = [\'foo\', \'foo
You can use list comprehension with the following one liner:
new_list = [x for n,x in zip(listb,lista) for _ in range(n)]
The code works as follows: first we generate a zip(listb,lista) which generates tuples of (2,'foo'), (3,'bar'), ... Now for each of these tuples we have a second for loop that iterates n times (for _ in range(n)) and thus we add x that many times to the list.
The _ is a variable that is usually used to denote that the value _ carries is not important: it is only used because we need a variable in the for loop to force iteration.