Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to compare a three tiered dictionary in python and only print values that dont match

I have two Dict as

RT = {'Solution 1': {'Returned Solution': 'Flight', 'Airline Class': 'Economy', 'Price': 'CAD 255.96'}, 'Solution 2': {'Returned Solution': 'Hotel', 'Hotel Name': 'Rosewood Hotel Georgia', 'Hotel Price': 'CAD 999', 'Cancellation Type': 'GUAR'}}

AA = {'Solution 1': {'Returned Solution': 'Flight', 'Airline Class': 'Main Cabin', 'Price': 'CAD 255.96'}, 'Solution 2': {'Returned Solution': 'Hotel', 'Hotel Name': 'Rosewood Hotel Georgia', 'Hotel Price': '(CAD 850 + fees)'}}

I am trying to compare this nested Dict and print Value that are not same.

Expecting output as:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Airline Class : 'Economy' 'Main Cabin'
Hotel Price : 'CAD 999' '(CAD 850 + fees)'
Cancellation Type: 'GUAR' 'N\A'

>Solution :

RT = {'Solution 1': {'Returned Solution': 'Flight', 'Airline Class': 'Economy', 'Price': 'CAD 255.96'}, 'Solution 2': {'Returned Solution': 'Hotel', 'Hotel Name': 'Rosewood Hotel Georgia', 'Hotel Price': 'CAD 999', 'Cancellation Type': 'GUAR'}}

AA = {'Solution 1': {'Returned Solution': 'Flight', 'Airline Class': 'Main Cabin', 'Price': 'CAD 255.96'}, 'Solution 2': {'Returned Solution': 'Hotel', 'Hotel Name': 'Rosewood Hotel Georgia', 'Hotel Price': '(CAD 850 + fees)'}}
ans = {}
for k,v in RT.items():
    cur = []
    for k_, v_ in v.items():
        if k_ in AA[k]:
            if (AA[k][k_]) != v_:
                cur.append(AA[k][k_])
                cur.append(v_)
                ans[k_] = cur
                cur = []
        else:
            cur.append(v_)
            cur.append("NA")
            ans[k_] = cur
            cur =[]
    
for k,v in ans.items():
    print(k, v)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading