I have two Dictionaries as mentioned below, I need to multiply each element in the list of dictionaries with respective element of the list of other dictionary and print result. I have managed to multiply one list, How do I make it dynamic?
dict1 = {0: [1, 1, 0, 1, 1, 0], 1: [1, 0, 1, 1, 1, 0]}
dict2 = { 0: [16, 0, 2, 0, 0, 0], 1: [15, 0, 0, 0, 1, 0]}
result = { 0: [16, 0, 0, 0, 0, 0], 1:[15, 0, 0, 0, 1, 0]}
from operator import mul
result = list( map(mul, dict1[0], dict2[0]) )
You can zip each of the lists together and use a dict comprehension like this:
result = {i :[x*y for x, y in zip(dict1[i], dict2[i])] for i in dict1.keys()}
This assumes that dict1 and dict2 share the same keys