Reputation: 93
I have two dictionaries with exaclty the same keys. The first dictionary is :
{ "key_1" : "AR" ,
"key_2":"BR" ,
"key_3" : "CR" }
the second is :
{ "key_1" : "signinfication of AR" ,
"key_2":" signinfication of BR" ,
"key_3" : " signinfication of CR" }
and I would like to obtain the dictionary below :
{"key_1" : {"AR" : "signinfication of AR"} ,
"key_2" : {"BR" : "signinfication of BR"} ,
"key_3" : {"CR" : "signinfication of CR"}
Thank you for your help !
Upvotes: 3
Views: 2198
Reputation: 402323
This is as simple as a one line dict comprehension.
>>> {k : {d1[k] : d2[k]} for k in d1.keys() & d2.keys()}
{
"key_2": {
"BR": " signinfication of BR"
},
"key_1": {
"AR": "signinfication of AR"
},
"key_3": {
"CR": " signinfication of CR"
}
}
Here, d1
and d2
are your two dictionaries. d1.keys() & d2.keys()
will perform an intersection on the dictionary keys to ensure that iteration is done over keys that exist in both the dictionaries. Here, that is
d1.keys() & d2.keys()
{'key_1', 'key_2', 'key_3'}
This is good to have in the general sense when you cannot guarantee that both dictionaries have the exact same keys.
On python2.7 and older, you'd need a slight modification, because keys()
returns a list. Use set.intersection
-
>>> {k : {d1[k] : d2[k]} for k in set(d1.keys()).intersection(d2.keys())}
If you're working with dicts of lists, then the code above requires a zip
ping between corresponding lists -
>>> d1
{
"key_1": [
"AR",
"BR",
"CR"
],
...
}
>>> d2
{
"key_1": [
"signfication of AR",
"signfication of BR",
"signfication of CR"
],
...
}
>>> {k : dict(zip(d1[k], d2[k])) for k in d1.keys() & d2.keys()}
{
"key_1": {
"BR": "signfication of BR",
"CR": "signfication of CR",
"AR": "signfication of AR"
},
"key_3": {
"CE": " signfication of CE",
"AE": "signfication of AE",
"BE": " signfication of BE"
},
"key_2": {
"BZ": "signfication of BZ",
"CZ": "signfication of CZ",
"AZ": "signfication of AZ"
}
}
Upvotes: 9
Reputation: 71451
You can try this:
s = { "key_1" : "AR" ,
"key_2":"BR" ,
"key_3" : "CR" }
d = { "key_1" : "signinfication of AR" ,
"key_2":" signinfication of BR" ,
"key_3" : " signinfication of CR" }
new_d = {a:{b:d[a]} for a, b in s.items()}
Output:
{'key_1': {'AR': 'signinfication of AR'}, 'key_3': {'CR': ' signinfication of CR'}, 'key_2': {'BR': ' signinfication of BR'}}
Upvotes: 1
Reputation: 26315
You could also zip()
together the dictionaries items()
, and merge them together:
d1 = {"key_1" : "AR",
"key_2":"BR",
"key_3" : "CR"}
d2 = {"key_1" : "signinfication of AR",
"key_2":" signinfication of BR",
"key_3" : " signinfication of CR"}
# make sure both lists have same ordered keys
l1 = sorted(d1.items())
l2 = sorted(d2.items())
d = {k1 : {v1:v2} for (k1, v1), (_, v2) in zip(l1, l2)}
print(d)
Which outputs:
{'key_1': {'AR': 'signinfication of AR'},
'key_2': {'BR': ' signinfication of BR'},
'key_3': {'CR': ' signinfication of CR'}}
EDIT:
As @cᴏʟᴅsᴘᴇᴇᴅ recommended, you can call sorted
on the lists before zipping them, which ensures that both dictionaries have the same order: key_1
, key_2
, key_3
. You could also do a preliminary check of the keys, such as checking their intersection, to ensure that both dictionaries have the same keys.
Upvotes: 2