Python:Comparing Two Dictionaries

Posted by saun jean on Stack Overflow See other posts from Stack Overflow or by saun jean
Published on 2012-10-23T03:24:10Z Indexed on 2012/10/23 5:04 UTC
Read the original article Hit count: 257

Filed under:

The first Dict is fixed.This Dict will remain as it is List of Countries with there Short Names.

firstDict={'ERITREA': 'ER', 'LAOS': 'LA', 'PORTUGAL': 'PT', "D'IVOIRE": 'CI', 'MONTENEGRO': 'ME', 'NEW CALEDONIA': 'NC', 'SVALBARD AND JAN MAYEN': 'SJ', 'BAHAMAS': 'BS', 'TOGO': 'TG', 'CROATIA': 'HR', 'LUXEMBOURG': 'LU', 'GHANA': 'GH'}

However This Tuple result has multiple Dict inside it.This is the format in which MySQLdb returns result:

result =({'count': 1L, 'country': 'Eritrea'}, {'count': 1L, 'country': 'Togo'}, {'count': 1L, 'country': 'Sierra Leone'}, {'count': 3L, 'country': 'Bahamas'}, {'count': 1L, 'country': 'Ghana'})

Now i want to compare these both results With COUNTRY Names and If 'Country' in Result is present in firstDict then put the value.else put the 0 The result desired is:

mainRes={'ER':1,'TG':1,'BS':3,'GH':0,'LU':0}

© Stack Overflow or respective owner

Related posts about python