I want to get an union-like merged dict of multiple (3 or more) dictionaries.
What I have:
d1 = {'key1': 'x1', 'key2': 'y1', 'key4': 'z1'}
d2 = {'key1': 'x2', 'key2': 'y2', 'key5': 'z2'}
d3 = {'key1': 'x3', 'key3': 'y3', 'key4': 'z3'}
What I want to get:
d_merged = {
'key1' : ('x1', 'x2', 'x3'),
'key2' : ('y1', 'y2', None),
'key3' : (None, None, 'y3'),
'key4' : ('z1', None, 'z3'),
'key5' : (None, 'z2', None)
}
What is the most pythonic / efficient way to to this?
I've found an example for 2 dictionaries here, but is there a better way (e.g.: a comprehension) what can solve the missing key problem for some dictionaries here?
Is there a way to do the same merge for any number of input dictionaries?