C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# Compare 2 dictionaries in python

By : Ed Maste
Date : November 19 2020, 07:01 PM
around this issue Python 2:
Assuming by 'entry' you mean 'key', a solution is to use set difference on the dictionary keys with something likee set(dict1.keys()) - set(dict2.keys())
code :
``````>>> a = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
>>> b = {'a': 1, 'c': 3}
>>> a.keys()
['a', 'c', 'b', 'd']
>>> b.keys()
['a', 'c']
>>> set(a.keys())-set(b.keys())
set(['b', 'd'])
>>>
``````
``````dict1.keys() - dict2.keys()
dict1.keys() ^ dict2.keys()
``````

Share :

## compare two dictionaries in python

By : user3142580
Date : March 29 2020, 07:55 AM
hop of those help? By recursion function for nested dictionary.
Get common keys from both dictionary by keys() and set operation. Iterate common keys by for loop. Check type of value of key is dict or not. If value type is dict then call same function and pass values dictionary as arguments. and add result as key into modified dictionary. If value type is not dict then add into modified dictionary.
code :
``````dict1 = {
'medic1' : {'date' : '02/02/2015', 'no' : '123' },
'medic2' : {'date' : '02/03/2015', 'no' : '456' },
'testkey1': 'testvalue1',
'testkey2': 'testvalue2',
'testkey3':{ "level2_1":"value2_1",
"level2_2":{
"level3_1": "value3_1_change",
"level3_2": "value3_2",
}
}
}

dict2 = {
'medic1' : {'date' : '02/02/2015', 'no' : '456' },
'medic2' : {'date' : '02/03/2015', 'no' : '456' },
'testkey1': 'testvalue1',
'testkey2': 'testvalue22',
'testkey3':{ "level2_1":"value2_1",
"level2_2":{
"level3_1": "value3_1",
"level3_2": "value3_2",
}
}

}

import copy

def compareDict(dict1, dict2):

d1_keys = dict1.keys()
d2_keys = dict2.keys()
intersect_keys = set(d1_keys).intersection(set(d2_keys))
modified = {}
for i in intersect_keys:
if dict1[i] != dict2[i] :
if isinstance(dict1[i], dict) and isinstance(dict1[i], dict):
modified[i]=compareDict(dict1[i], dict2[i])
else:
modified.update({i : (dict1[i], dict2[i])})

return copy.deepcopy(modified)

modified = compareDict(dict1, dict2)
import pprint
pprint.pprint(modified)
``````
``````vivek@vivek:~/Desktop/stackoverflow\$ python 5.py
{'medic1': {'no': ('123', '456')},
'testkey2': ('testvalue2', 'testvalue22'),
'testkey3': {'level2_2': {'level3_1': ('value3_1_change', 'value3_1')}}}
``````

## how to compare two dictionaries in python

By : user1928810
Date : March 29 2020, 07:55 AM
Does that help Here's a simplified version of your problem. You can group your AwardsDict "value" dictionaries by NSN and then compare and match.
code :
``````RFQDict = {
}
AwardsDict = {
'W21': {'Awarddate': '08-13-2007', 'Awardid': '1', 'NSN': '9906'},
'W22': {'Awarddate': '08-14-2007', 'Awardid': '2', 'NSN': '9905'},
'W23': {'Awarddate': '08-15-2007', 'Awardid': '3', 'NSN': '9908'},
'W24': {'Awarddate': '08-16-2007', 'Awardid': '4', 'NSN': '9907'},
}

# First create a new dictionary with "NSN" as keys and awards as matches
nsn_awards = {v['NSN']: v for v in AwardsDict.values()}

# go through all values of RFQDict and find a match by
# looking up the its NSN in the nsn_awards_dict
matches = [(rfq, nsn_awards[rfq['NSN']]) for rfq in RFQDict.values()]

print(matches)
``````
``````[({'NSN': '9908', 'Add_method': 'D4'}, {'NSN': '9908', 'Awardid': '3', 'Awarddate': '08-15-2007'}), ({'NSN': '9907', 'Add_method': 'D3'}, {'NSN': '9907', 'Awardid': '4
', 'Awarddate': '08-16-2007'}), ({'NSN': '9906', 'Add_method': 'D2'}, {'NSN': '9906', 'Awardid': '1', 'Awarddate': '08-13-2007'}), ({'NSN': '9905', 'Add_method': 'D1'}
, {'NSN': '9905', 'Awardid': '2', 'Awarddate': '08-14-2007'})]
``````
``````matches = []
for rfq in RFQDict.values():
if rfq['NSN'] in nsn_awards: # only append if there's a match
nsn = rfq['NSN']
matches.append((rfq, nsn_awards[nsn]))
``````

## Compare dictionaries using python

By : Ida Jaa
Date : March 29 2020, 07:55 AM
wish helps you Use dictionary view objects and use set operations:

## Python: Compare values from 2 dictionaries with the same key

By : STIJN
Date : March 29 2020, 07:55 AM
this one helps. If what you want is to see how many of any given car can be created without actually affecting the contents of Materials, you could write your method like so:

## Compare dictionaries in python

By : h5s5
Date : March 29 2020, 07:55 AM
may help you . Well, for one, you get KeyError because you try to look up keys which don't exist in both dicionaries.
It sounds to me like you want to compute an intersection of two dictionaries. In such case, it's enough to:
code :
``````>>> a = dict(a=1, b=2, c=3)
>>> b = dict(b=2, c=3, d=4)
>>> dict(a.items() & b.items())
{'c': 3, 'b': 2}
``````