Merge two dicts by same key [duplicate]

Clash Royale CLAN TAG#URR8PPP
This question already has an answer here:
Merging dictionary value lists in python
3 answers
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?
python list dictionary
marked as duplicate by coldspeed
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 9 at 18:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Merging dictionary value lists in python
3 answers
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?
python list dictionary
marked as duplicate by coldspeed
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 9 at 18:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Are we sure that bothd1andd2have same set of keys?
– cph_sto
Jan 9 at 12:39
In this example and according to my present needs, yes. Of course I'm sure that with different sets of keys in the two dictionaries the code will be different.
– Ric S
Jan 9 at 13:03
add a comment |
This question already has an answer here:
Merging dictionary value lists in python
3 answers
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?
python list dictionary
This question already has an answer here:
Merging dictionary value lists in python
3 answers
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?
This question already has an answer here:
Merging dictionary value lists in python
3 answers
python list dictionary
python list dictionary
edited Jan 9 at 13:25
yatu
6,6111826
6,6111826
asked Jan 9 at 11:32
Ric SRic S
657317
657317
marked as duplicate by coldspeed
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 9 at 18:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by coldspeed
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Jan 9 at 18:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Are we sure that bothd1andd2have same set of keys?
– cph_sto
Jan 9 at 12:39
In this example and according to my present needs, yes. Of course I'm sure that with different sets of keys in the two dictionaries the code will be different.
– Ric S
Jan 9 at 13:03
add a comment |
Are we sure that bothd1andd2have same set of keys?
– cph_sto
Jan 9 at 12:39
In this example and according to my present needs, yes. Of course I'm sure that with different sets of keys in the two dictionaries the code will be different.
– Ric S
Jan 9 at 13:03
Are we sure that both
d1 and d2 have same set of keys?– cph_sto
Jan 9 at 12:39
Are we sure that both
d1 and d2 have same set of keys?– cph_sto
Jan 9 at 12:39
In this example and according to my present needs, yes. Of course I'm sure that with different sets of keys in the two dictionaries the code will be different.
– Ric S
Jan 9 at 13:03
In this example and according to my present needs, yes. Of course I'm sure that with different sets of keys in the two dictionaries the code will be different.
– Ric S
Jan 9 at 13:03
add a comment |
5 Answers
5
active
oldest
votes
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]
2
Damn, it was the plus sign, not the comma.. I knew it was easy but it didn't come to mind! Thanks
– Ric S
Jan 9 at 11:38
add a comment |
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
add a comment |
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]
add a comment |
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]
this fails if there are keys ind1that are not ind2, andd2_keys_not_in_d1can be expressed simpler asd2.keys() - d1.keys()
– Maarten Fabré
Jan 9 at 13:31
Thanks Maarten for your feedback. Very appreciated. I have changed the code accordingly and used your expression, which was lot more succinct never the less.
– cph_sto
Jan 9 at 14:02
the common keys can be expressed as ` d2.keys() & d1.keys()`
– Maarten Fabré
Jan 9 at 14:14
Thanks a lot Maarten. Very helpful. I have learnt something :)
– cph_sto
Jan 9 at 14:16
add a comment |
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.
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
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]
2
Damn, it was the plus sign, not the comma.. I knew it was easy but it didn't come to mind! Thanks
– Ric S
Jan 9 at 11:38
add a comment |
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]
2
Damn, it was the plus sign, not the comma.. I knew it was easy but it didn't come to mind! Thanks
– Ric S
Jan 9 at 11:38
add a comment |
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 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]
answered Jan 9 at 11:35
yatuyatu
6,6111826
6,6111826
2
Damn, it was the plus sign, not the comma.. I knew it was easy but it didn't come to mind! Thanks
– Ric S
Jan 9 at 11:38
add a comment |
2
Damn, it was the plus sign, not the comma.. I knew it was easy but it didn't come to mind! Thanks
– Ric S
Jan 9 at 11:38
2
2
Damn, it was the plus sign, not the comma.. I knew it was easy but it didn't come to mind! Thanks
– Ric S
Jan 9 at 11:38
Damn, it was the plus sign, not the comma.. I knew it was easy but it didn't come to mind! Thanks
– Ric S
Jan 9 at 11:38
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Jan 9 at 13:29
Maarten FabréMaarten Fabré
4,9901822
4,9901822
add a comment |
add a comment |
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]
add a comment |
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]
add a comment |
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]
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]
answered Jan 9 at 11:38
Daniel MesejoDaniel Mesejo
16.5k21430
16.5k21430
add a comment |
add a comment |
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]
this fails if there are keys ind1that are not ind2, andd2_keys_not_in_d1can be expressed simpler asd2.keys() - d1.keys()
– Maarten Fabré
Jan 9 at 13:31
Thanks Maarten for your feedback. Very appreciated. I have changed the code accordingly and used your expression, which was lot more succinct never the less.
– cph_sto
Jan 9 at 14:02
the common keys can be expressed as ` d2.keys() & d1.keys()`
– Maarten Fabré
Jan 9 at 14:14
Thanks a lot Maarten. Very helpful. I have learnt something :)
– cph_sto
Jan 9 at 14:16
add a comment |
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]
this fails if there are keys ind1that are not ind2, andd2_keys_not_in_d1can be expressed simpler asd2.keys() - d1.keys()
– Maarten Fabré
Jan 9 at 13:31
Thanks Maarten for your feedback. Very appreciated. I have changed the code accordingly and used your expression, which was lot more succinct never the less.
– cph_sto
Jan 9 at 14:02
the common keys can be expressed as ` d2.keys() & d1.keys()`
– Maarten Fabré
Jan 9 at 14:14
Thanks a lot Maarten. Very helpful. I have learnt something :)
– cph_sto
Jan 9 at 14:16
add a comment |
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]
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]
edited Jan 9 at 14:15
answered Jan 9 at 11:57
cph_stocph_sto
1,548320
1,548320
this fails if there are keys ind1that are not ind2, andd2_keys_not_in_d1can be expressed simpler asd2.keys() - d1.keys()
– Maarten Fabré
Jan 9 at 13:31
Thanks Maarten for your feedback. Very appreciated. I have changed the code accordingly and used your expression, which was lot more succinct never the less.
– cph_sto
Jan 9 at 14:02
the common keys can be expressed as ` d2.keys() & d1.keys()`
– Maarten Fabré
Jan 9 at 14:14
Thanks a lot Maarten. Very helpful. I have learnt something :)
– cph_sto
Jan 9 at 14:16
add a comment |
this fails if there are keys ind1that are not ind2, andd2_keys_not_in_d1can be expressed simpler asd2.keys() - d1.keys()
– Maarten Fabré
Jan 9 at 13:31
Thanks Maarten for your feedback. Very appreciated. I have changed the code accordingly and used your expression, which was lot more succinct never the less.
– cph_sto
Jan 9 at 14:02
the common keys can be expressed as ` d2.keys() & d1.keys()`
– Maarten Fabré
Jan 9 at 14:14
Thanks a lot Maarten. Very helpful. I have learnt something :)
– cph_sto
Jan 9 at 14:16
this fails if there are keys in
d1 that are not in d2, and d2_keys_not_in_d1 can be expressed simpler as d2.keys() - d1.keys()– Maarten Fabré
Jan 9 at 13:31
this fails if there are keys in
d1 that are not in d2, and d2_keys_not_in_d1 can be expressed simpler as d2.keys() - d1.keys()– Maarten Fabré
Jan 9 at 13:31
Thanks Maarten for your feedback. Very appreciated. I have changed the code accordingly and used your expression, which was lot more succinct never the less.
– cph_sto
Jan 9 at 14:02
Thanks Maarten for your feedback. Very appreciated. I have changed the code accordingly and used your expression, which was lot more succinct never the less.
– cph_sto
Jan 9 at 14:02
the common keys can be expressed as ` d2.keys() & d1.keys()`
– Maarten Fabré
Jan 9 at 14:14
the common keys can be expressed as ` d2.keys() & d1.keys()`
– Maarten Fabré
Jan 9 at 14:14
Thanks a lot Maarten. Very helpful. I have learnt something :)
– cph_sto
Jan 9 at 14:16
Thanks a lot Maarten. Very helpful. I have learnt something :)
– cph_sto
Jan 9 at 14:16
add a comment |
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.
add a comment |
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.
add a comment |
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 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.
answered Jan 9 at 11:36
jppjpp
97.3k2159109
97.3k2159109
add a comment |
add a comment |
Are we sure that both
d1andd2have same set of keys?– cph_sto
Jan 9 at 12:39
In this example and according to my present needs, yes. Of course I'm sure that with different sets of keys in the two dictionaries the code will be different.
– Ric S
Jan 9 at 13:03