I have 2 lists: list1
and list2
list1
is a list in list and contains various strings
list2
is also a list in list and consists of different integer values
the dimensions of both lists is the same
how do I multiply each string in list1
with each integer in list2
in order to achieve something like that:
list1 = [['ABC','DEF'],['GHI','JKL'],['MNO','PQR']]
list2 = [[2, 2], [3, 3], [4, 4]]
result = [['ABC', 'ABC', 'DEF', 'DEF'], ['GHI', 'GHI', 'GHI', 'JKL', 'JKL', 'JKL'], ...]
Thank you very much
I already tried the zip() function and worked with the * operator but nothing worked
Expecting that sublists in list2
can have varying values. I coded this:
list1 = [['ABC','DEF'],['GHI','JKL'],['MNO','PQR']]
list2 = [[2, 2], [3, 3], [4, 4], [1, 1]]
combined = []
for sublist1, sublist2 in zip(list1, list2):
sublist = []
for elem1, elem2 in zip(sublist1, sublist2):
sublist.extend([elem1]* elem2)
combined.append(sublist)
print(combined)
Result:
[['ABC', 'ABC', 'DEF', 'DEF'], ['GHI', 'GHI', 'GHI', 'JKL', 'JKL', 'JKL'], ['MNO', 'MNO', 'MNO', 'MNO', 'PQR', 'PQR', 'PQR', 'PQR']]