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 zipping 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"
}
}