I have two dictionaries and need to combine the values of similar keys in them. Here's an example:
dict1 = {'key1':[value11,value12,value13] , 'key2':[value21,value22,value23]}
dict2 = {'key1':[value14,value15] , 'key2':[value24,value25]}
I used :
dict3 = {}
for key in (dict1.viewkeys() | dict2.keys()):
if key in dict1: dict3.setdefault(key, []).append(dict1[key])
if key in dict2: dict3.setdefault(key, []).append(dict2[key])
which gives me:
dict3 = {'key1':[[value11,value12,value13],[value14,value15]] , 'key2':[[value21,value22,value23],[value24,value25]]}
What I want is a simple one like:
Desired output :
dict3 = {'key1':[value11,value12,value13,value14,value15] , 'key2':[value21,value22,value23,value24,value25]}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…