[Solved] How to multiply list by integer within dictionary?


Your code is duplicating every corresponding list (values) in example1 as many times as the values in example2.

Your code is similar to:

>>>>two_items = ["A","B"]
>>>>number = [3]
>>>>result = two_items*number[0]

['A', 'B', 'A', 'B', 'A', 'B']

To make this clear, it works like string multiplication:

>>>>my_string="Hello "
>>>>print(my_string * number[0])
Hello Hello Hello 

What you need to do is to iterate through each items in the list and multiply it by a given number, as following:

>>>>two_numbers=[1,2]
>>>>number=[3]
>>>>result=[]
>>>>for num in two_numbers:
>>>>    x =num * number[0]
>>>>    result.append(x)
[3, 6]

Given the above, your code should look like that:

example3 = {}
for key in example2.keys():
    temp =[]
    for item in example[key]:   
        temp.append(item*example2[key])
    example3[key]=temp

solved How to multiply list by integer within dictionary?