I have two dictionaries and a merged dictionary:
dict1 = {-3: 0.3, -2: 0.1, 1: 0.8} dict2 = {0: 0.3, 1: 0.5, -1: 0.7} dict_merged = {}
I have code that basically merges the two together by adding keys together and multiplying values: e.g.
for k1, v1 in dict1.items(): for k2, v2 in dict2.items(): new_key = k1 + k2 if new_key in dict_merged: dict_merged[new_key] += v1 * v2 else: dict_merged[new_key] = v1 * v2
Is there a faster way to do this, it feels like there should but dict comprehension doesn't seem helpful here. In the above example, we are going to get several contributes to the key "-3" for example.