Home > OS >  How to Compare nested Dictionaries and update missing items in python?
How to Compare nested Dictionaries and update missing items in python?

Time:06-07

Hai needs to compare two dictionaries. How to Find and update the missing items to the base dictionary.

language_1 = {"kannada" : {"lbl01":"kannada_File","lbl02":"kannada_accounts","lbl03":"kannada_Inventory","lbl04":"kannada_manufacture"},
          "english": {"lbl01": "File", "lbl02": "Accounts", "lbl03": "Inventory", "lbl04": "manufacture"}}
          

language = {"id"      : {"lbl01":"file"      ,"lbl02":"accounts"     ,"lbl03":"inventory"       ,"lbl04":"manufacture"},
            "english" : {"lbl01":"File"      ,"lbl02":"Accounts"     ,"lbl03":"Inventory"       ,"lbl04":"manufacture"},
            "tamil"   : {"lbl01":"கோப்பு"    ,"lbl02":"கணக்கியல்"   ,"lbl03":"சரக்கியல்"       ,"lbl04":"உற்ப்பத்தி"},
            "hindi"   : {"lbl01":"Hindi_File","lbl02":"Hindi_accounts","lbl03":"Hindi_Inventory","lbl04":"Hindi_manufacture"}}

For example "language" is my base dictionary. I want to check, whether the items in "langugae_1" is found in a base dictionary or not. If Not found, then add it into the base dictionary. In my case "kannada" is not found in my base dictionary, so add it into the base dictionary. How to resolve it?

CodePudding user response:

It seems a simple question. the dictionary has the .update() function.

language.update(language_1)

or if you need to apply some filter-logic

language.update({k:v for k,v in language_1.items() if <some logic>})

here some example and here

If you need some sophisticated logic, dictdiffer can be a very useful pakcage

CodePudding user response:

Simple and understandable way.


language_1 = {"kannada" : {"lbl01":"kannada_File","lbl02":"kannada_accounts","lbl03":"kannada_Inventory","lbl04":"kannada_manufacture"},
          "english": {"lbl01": "File", "lbl02": "Accounts", "lbl03": "Inventory", "lbl04": "manufacture"}}


language = {"id"      : {"lbl01":"file"      ,"lbl02":"accounts"     ,"lbl03":"inventory"       ,"lbl04":"manufacture"},
            "english" : {"lbl01":"File"      ,"lbl02":"Accounts"     ,"lbl03":"Inventory"       ,"lbl04":"manufacture"},
            "tamil"   : {"lbl01":"கோப்பு"    ,"lbl02":"கணக்கியல்"   ,"lbl03":"சரக்கியல்"       ,"lbl04":"உற்ப்பத்தி"},
            "hindi"   : {"lbl01":"Hindi_File","lbl02":"Hindi_accounts","lbl03":"Hindi_Inventory","lbl04":"Hindi_manufacture"}}

for a in language_1:
    if not any(language[b] == language_1[a] for b in language):
        count = list(language).count(a)
        if count:
            language[a str(count)] = language_1[a]
        else:
            language[a] = language_1[a]

print(language)

I know a simple way is language.update(language_1) but If you want to check Every nested dictionary then use this.

Means

d = {'a':[1,2,3]}

full_d = {'a':[1,2]}

# Here full_d['a'] != d['a'], THen this make another key but with the serial number.

# OUTPUT

{'a':[1,2],'a1':[1,2,3]}

OR Try this (Suggested First One)

for a in language_1:
    if not any(language[b] == language_1[a] for b in language):
        count = list(language).count(a)
        if count == 1:
            language[a str(count)] = language[a]
            del language[a]
        if count:
            language[a str(count 1)] = language_1[a]
        else:
            language[a] = language_1[a]

print(language)
d = {'a':[1,2,3]}

full_d = {'a':[1,2]}

# Here full_d['a'] != d['a'], THen this make another key but with the serial number.

# OUTPUT

{'a1':[1,2],'a2':[1,2,3]}

CodePudding user response:

Quite a simple answer:

language.update(language_1)

CodePudding user response:

There is an inbuilt python dictionary method to do exactly the same You can use dict.update() method to achieve this.

In your case. First you need to copy your data from language_1 to some variable(If you want to keep language_1 intact)

temp = language_1.copy()

Then you can perform update

temp.update(language)

Copy the data to language variable

language = temp.copy()

CodePudding user response:

You can just simply assign it like this

for key in language_1.keys():
if key not in language.keys():
    language[key]=language_1[key]
  • Related