3

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 !

0

3 Answers 3

9

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"
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

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.

3 Comments

Can you guarantee that the keys are returned from both dicts in the same order?
@cᴏʟᴅsᴘᴇᴇᴅ hmm I didn't think of that, sorry. I guess the OP could just use your recommendation in your answer to make sure this happens. I'll add this anyways, thanks.
It's not a big deal to guarantee that, just call sorted and it should work. Upvoted, cheers.
1

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'}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.