I have multiple nested dictionaries with different levels and I would like to combine them on the same key. Here, I am sharing with 3 examples such as:
dict_1={'D': {'D': '1','B': '2','A': '3'},'A': {'A': '5','J': '6'}}
dict_2={'D': {'D': '7', 'B': '8', 'C': '9'},'A': {'A': '12', 'C':'13'}}
dict_3={'D': {'test1': '14','test2': '3'},'B': {'test1': '21','test2': '16'},'A': {'test1': '3','test2': '2'},'J': {'test1': '15','test2': '3'}, 'C':{'test1': '44','test2': '33'}}
I want to combine these 3 as by adding 'dict_3' keys (adding the information from dict_3) and values to the combination of 'dict_1' and 'dict_2' for each key:
main_dict={
'D':
{'D':{'dict_1_value':'1', 'dict_2_value':'7', 'test1': '14', 'test2': '3'},
'B':{'dict_1_value':'2', 'dict_2_value':'8', 'test1': '21', 'test2': '16'},
'A':{'dict_1_value':'3', 'test1': '3', 'test2': '2'},
'C':{'dict_2_value':'9', 'test1': '44', 'test2': '33'}},
'A':
{'A':{'dict_1_value':'5', 'dict_2_value':'12', 'test1': '3', 'test2': '2'},
'J':{'dict_1_value':'6', 'test1': '15', 'test2': '3'},
'C':{'dict_2_value':'13', 'test1': '44', 'test2': '33'}}
}
At first, I have tried to combine dict_1 and dict_2 but I am overwriting the values from dict_1 when I tried such as {k: v | dict_2[k] for k, v in dict_1.items()}
or dict(**dict_1,**dict_2)
. Moreover, I don't know how I can add dict_3 by adding key name as 'dict_1_value' or 'dict_2_value'.
Is there any way to accomplish main_dict?
CodePudding user response:
all_keys = set(dict_1.keys()).union(dict_2.keys())
temp_1 = {key: {k: {'dict_1_value': v} for k, v in sub.items()} for key, sub in dict_1.items()}
temp_2 = {key: {k: {'dict_2_value': v} for k, v in sub.items()} for key, sub in dict_2.items()}
combined = {}
for key in all_keys:
sub_1 = temp_1.get(key, {})
sub_2 = temp_2.get(key, {})
sub_keys = set(sub_1.keys()).union(sub_2.keys())
combined[key] = {k: sub_1.get(k, {}) | sub_2.get(k, {}) for k in sub_keys}
Now there are 2 options:
1
Dictionary comprehension, but the new dictionary is constructed from zero:
main_dict = {key: {k: sub[k] | dict_3.get(k, {})
for k, v in sub.items()}
for key, sub in combined.items()}
2
Loop, but the items of the existing dictionary are just updated:
for key, sub in combined.items():
for k, v in sub.items():
v.update(dict_3.get(k, {}))
main_dict = combined