Reputation: 1923
Lets say I have 2 dictionaries:
dict_a = {'01A': 'X', '02B': 'V', '03C': 'AE'}
dict_b = {'01A': 'V', '02B': 'D', '03C': 'X'}
They essentially have the same keys. What I want is this:
dict_c = {'01A': ['X', 'V'], '02B': ['V', 'D'], '03C': ['AE', 'X']}
What is the proper way to do this?
Upvotes: 0
Views: 10
Reputation: 4380
There are many ways to achieve that, the one could be using defaultdict
from collections
something like this.
from collections import defaultdict
dict_a = {'01A': 'X', '02B': 'V', '03C': 'AE'}
dict_b = {'01A': 'V', '02B': 'D', '03C': 'X'}
d = defaultdict(list)
for d1, d2 in dict_a.items() + dict_b.items():
d[d1].append(d2)
print(dict(d))
Upvotes: 1