2

Hi I have two dictionary as follows

{'abc':1,'xyz':8,'pqr':9,'ddd': 22}
{0:'pqr',1:'xyz',2:'abc',3:'ddd'}

My objective is to get a new dictionary in the following format

{2:1 1:8 0:9 3:22}

I am combing the value of first dictionary as value of the new dictionary and the key of dictionary 2 whose value matches with the key of the dictionary 1 as key of the the new dictionary.

I have written some code as follows:

for list1elem in listofemail[1:]:
    print(list1elem)
    for the_key, the_value in list1elem.items():
        the_key = [k for k, v in vocab_dic.items() if v == the_key]

But my code is not replacing the old key with the new one. Both of my dictionaries are large, containing 25000 key/value pair. So it is taking a lot of time. What would be the fastest way to do this?

2
  • there is a syntax error in the second dictionary. Remove the extra comma Commented Sep 26, 2015 at 19:42
  • What do you mean by "combing" here? Commented Sep 26, 2015 at 20:12

2 Answers 2

6

this

d1 = {'abc':1, 'xyz':8, 'pqr':9, 'ddd':22}
d2 = {0:'pqr', 1:'xyz', 2:'abc', 3:'ddd'}

d = {k:d1[v] for k,v in d2.items()}

produces

{0: 9, 1: 8, 2: 1, 3: 22}

Basically, it goes through every item (key and value) in d2 and it uses the value v as the key into d1 to get its corresponding value. It then combines the latter with the original key k to create the item that goes into the resulting dictionary.

Side note: there is no error checking. It assumes the values in d2 are present in d1 as keys.

In case you wanted a specific value if the key is missing (e.g. -1), you can do

d = {k:d1.get(v, -1) for k,v in d2.items()}

Otherwise, in case you wanted to omit inserting the item altogether, use

d = {k:d1[v] for k,v in d2.items() if v in d1}
Sign up to request clarification or add additional context in comments.

5 Comments

@zstewart yes, you beat me to it, I am adding the options. BTW your code is flawed: it's v that must be in d1 and dictionaries give back keys only as an iterable (use .items instead)
what should I do if my dictionary two has some more value which dictionary want doesn't have.
Well, that is up to you to decide. At the bottom of the answer, I give you two options: you can either insert a fixed value with the missing key or otherwise avoid inserting such key altogether (i.e. skip it)
its woking fine now.The only problem I am having now is actually I have a list of dictionary as follows
@akira, Please accept sufficient answers with the checkmark button. It's worth +2 rep to you.
4

You can do the following:

In [1]: a1 = {'abc':1,'xyz':8,'pqr':9,'ddd': 22} 

In [2]: a2 = {0:'pqr', 1:'xyz',2:'abc',3:'ddd'}

In [3]: a3 = {}

In [4]: for key, value in a2.items():
            # extra checking if the value of 2nd dictionary is a key in 1st dictionary
            if value in a1: 
                a3[key] = a1[value]    

In [5]: a3 
Out[5]: {0: 9, 1: 8, 2: 1, 3: 22} # desired result

Here we iterate over the 2nd dictionary and check if values of 2nd dictionary are present as keys in the 1st dictionary using the in operator. If they are present, then we assign the key of the 2nd dictionary as the key of a3 and value as the value obtained on performing lookup in the 1st dictionary with the value of 2nd dictionary.

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.