Merge two dicts by same key
Question:
I have the following two toy dicts
d1 = {
'a': [2,4,5,6,8,10],
'b': [1,2,5,6,9,12],
'c': [0,4,5,8,10,21]
}
d2 = {
'a': [12,15],
'b': [14,16],
'c': [23,35]
}
and I would like get a unique dictionary where I stack the second dictionary values after the first ones, within the same square brackets.
I tried the following code
d_comb = {key:[d1[key], d2[key]] for key in d1}
but the output I obtain has two lists within a list for each key, i.e.
{'a': [[2, 4, 5, 6, 8, 10], [12, 15]],
'b': [[1, 2, 5, 6, 9, 12], [14, 16]],
'c': [[0, 4, 5, 8, 10, 21], [23, 35]]}
whereas I would like to obtain
{'a': [2, 4, 5, 6, 8, 10, 12, 15],
'b': [1, 2, 5, 6, 9, 12, 14, 16],
'c': [0, 4, 5, 8, 10, 21, 23, 35]}
How can I do that with a line or two of code?
Answers:
You almost had it, instead use +
to append both lists:
{key: d1[key] + d2[key] for key in d1}
{'a': [2, 4, 5, 6, 8, 10, 12, 15],
'b': [1, 2, 5, 6, 9, 12, 14, 16],
'c': [0, 4, 5, 8, 10, 21, 23, 35]}
You can use itertools.chain
to efficiently construct a single list from input lists:
from itertools import chain
d_comb = {key: list(chain(d1[key], d2[key])) for key in d1}
For the more general case covering an arbitrary number of dictionaries and keys which are not equal across dictionaries, see Merging dictionary value lists in python.
You could use extended iterable unpacking:
d1 = {
'a': [2,4,5,6,8,10],
'b': [1,2,5,6,9,12],
'c': [0,4,5,8,10,21]
}
d2 = {
'a': [12,15],
'b': [14,16],
'c': [23,35]
}
d_comb = {key:[*d1[key], *d2[key]] for key in d1}
print(d_comb)
Output
{'c': [0, 4, 5, 8, 10, 21, 23, 35], 'b': [1, 2, 5, 6, 9, 12, 14, 16], 'a': [2, 4, 5, 6, 8, 10, 12, 15]}
The code will work irrespective of whether d1
or d2
have the same set of keys. I have added a key 'e'
in d1
and 'd'
in d2
.
d1 = {'a': [2, 4, 5, 6, 8, 10], 'b': [1, 2, 5, 6, 9, 12], 'c': [0, 4, 5, 8, 10, 21], 'e':[0,0,0]}
d2 = {'a': [12, 15], 'b': [14, 16], 'c': [23, 35], 'd': [13, 3]}
d2_keys_not_in_d1 = d2.keys() - d1.keys()
d1_keys_not_in_d2 = d1.keys() - d2.keys()
common_keys = d2.keys() & d1.keys()
for i in common_keys:
d[i]=d1[i]+d2[i]
for i in d1_keys_not_in_d2:
d[i]=d1[i]
for i in d2_keys_not_in_d1:
d[i]=d2[i]
d
{'a': [2, 4, 5, 6, 8, 10, 12, 15],
'b': [1, 2, 5, 6, 9, 12, 14, 16],
'c': [0, 4, 5, 8, 10, 21, 23, 35],
'd': [13, 3],
'e': [0, 0, 0]}
if not all the keys from d2
are in d1
, then the simplest thing is using set union and dict.get
:
combined_keys = d1.keys() | d2.keys()
d_comb = {key: d1.get(key, []) + d2.get(key, []) for key in combined_keys}
I have the following two toy dicts
d1 = {
'a': [2,4,5,6,8,10],
'b': [1,2,5,6,9,12],
'c': [0,4,5,8,10,21]
}
d2 = {
'a': [12,15],
'b': [14,16],
'c': [23,35]
}
and I would like get a unique dictionary where I stack the second dictionary values after the first ones, within the same square brackets.
I tried the following code
d_comb = {key:[d1[key], d2[key]] for key in d1}
but the output I obtain has two lists within a list for each key, i.e.
{'a': [[2, 4, 5, 6, 8, 10], [12, 15]],
'b': [[1, 2, 5, 6, 9, 12], [14, 16]],
'c': [[0, 4, 5, 8, 10, 21], [23, 35]]}
whereas I would like to obtain
{'a': [2, 4, 5, 6, 8, 10, 12, 15],
'b': [1, 2, 5, 6, 9, 12, 14, 16],
'c': [0, 4, 5, 8, 10, 21, 23, 35]}
How can I do that with a line or two of code?
You almost had it, instead use +
to append both lists:
{key: d1[key] + d2[key] for key in d1}
{'a': [2, 4, 5, 6, 8, 10, 12, 15],
'b': [1, 2, 5, 6, 9, 12, 14, 16],
'c': [0, 4, 5, 8, 10, 21, 23, 35]}
You can use itertools.chain
to efficiently construct a single list from input lists:
from itertools import chain
d_comb = {key: list(chain(d1[key], d2[key])) for key in d1}
For the more general case covering an arbitrary number of dictionaries and keys which are not equal across dictionaries, see Merging dictionary value lists in python.
You could use extended iterable unpacking:
d1 = {
'a': [2,4,5,6,8,10],
'b': [1,2,5,6,9,12],
'c': [0,4,5,8,10,21]
}
d2 = {
'a': [12,15],
'b': [14,16],
'c': [23,35]
}
d_comb = {key:[*d1[key], *d2[key]] for key in d1}
print(d_comb)
Output
{'c': [0, 4, 5, 8, 10, 21, 23, 35], 'b': [1, 2, 5, 6, 9, 12, 14, 16], 'a': [2, 4, 5, 6, 8, 10, 12, 15]}
The code will work irrespective of whether d1
or d2
have the same set of keys. I have added a key 'e'
in d1
and 'd'
in d2
.
d1 = {'a': [2, 4, 5, 6, 8, 10], 'b': [1, 2, 5, 6, 9, 12], 'c': [0, 4, 5, 8, 10, 21], 'e':[0,0,0]}
d2 = {'a': [12, 15], 'b': [14, 16], 'c': [23, 35], 'd': [13, 3]}
d2_keys_not_in_d1 = d2.keys() - d1.keys()
d1_keys_not_in_d2 = d1.keys() - d2.keys()
common_keys = d2.keys() & d1.keys()
for i in common_keys:
d[i]=d1[i]+d2[i]
for i in d1_keys_not_in_d2:
d[i]=d1[i]
for i in d2_keys_not_in_d1:
d[i]=d2[i]
d
{'a': [2, 4, 5, 6, 8, 10, 12, 15],
'b': [1, 2, 5, 6, 9, 12, 14, 16],
'c': [0, 4, 5, 8, 10, 21, 23, 35],
'd': [13, 3],
'e': [0, 0, 0]}
if not all the keys from d2
are in d1
, then the simplest thing is using set union and dict.get
:
combined_keys = d1.keys() | d2.keys()
d_comb = {key: d1.get(key, []) + d2.get(key, []) for key in combined_keys}