I'm trying to write a function that merges the items in multiple dictionaries under a single dictionary for matching keys.
For example, given
dict1 = {1:3, 2:4}
dict2 = {1:5, 2:6}
the function must return:
dict3 = {1:(3,5),2:(4,6)}
I also want the values to be numerically sorted, i.e. like this:
{1:(3,5)}
instead of this:
{1:(5,3)}
So far, I tried this, but it didn't work as I expected:
def mergeDicts(dict1, dict2):
dict3 = {**dict1, **dict2}
for key, value in dict3.items():
if key in dict1 and key in dict2:
dict3[key] = (value , dict1[key])
return dict3
{k: (d1[k], d2[k]) for k in d1}