comparing two dictionaries in python

Solutions on MaxInterview for comparing two dictionaries in python by the best coders in the world

showing results for - "comparing two dictionaries in python"
Alain
07 Jul 2018
1def dict_compare(d1, d2):
2    d1_keys = set(d1.keys())
3    d2_keys = set(d2.keys())
4    shared_keys = d1_keys.intersection(d2_keys)
5    added = d1_keys - d2_keys
6    removed = d2_keys - d1_keys
7    modified = {o : (d1[o], d2[o]) for o in shared_keys if d1[o] != d2[o]}
8    same = set(o for o in shared_keys if d1[o] == d2[o])
9    return added, removed, modified, same
10
11x = dict(a=1, b=2)
12y = dict(a=2, b=2)
13added, removed, modified, same = dict_compare(x, y)
Benjamín
25 Nov 2020
1value = { k : second_dict[k] for k in set(second_dict) - set(first_dict) }
2
María
22 Sep 2020
1In [1]: from deepdiff import DeepDiff
2
3In [2]: a = {
4   ...:     'number': 1,
5   ...:     'list': ['one', 'two']
6   ...: }
7
8In [3]: b = {
9   ...:     'list': ['one', 'two'],
10   ...:     'number': 2
11   ...: }
12
13In [4]: diff = DeepDiff(a, b)
14
15In [5]: diff
16Out[5]: {'values_changed': {"root['number']": {'new_value': 2, 'old_value': 1}}}
17
queries leading to this page
how to compare type of two dict in pythoncomparing values in dictionary pythoncompare two dictionaries python by valueshow to compare to list of dicts in pythonpython compare dictshow to compare two dictionary values in pythonpython dict compare 1 dict against many dictpython compare 3 dictionary valuespython check if two dictionaries are equalcompare dictionaries in pythoncompare dictionaries with selected keyscan we compare two dictionaries in pythonhow to compare two dictionaries for similarities in pythonhow to compare structure of two dicts in pythonpython dictionary compare key valuepython dictionary comparecompare keys of dictionary pythonpython dictionary comparisonpython 3 compare dictionariesfastest way to compare two dicts in pythoncompare items in two dicts pythondict comparison compare value python dictionary in dictionarycompare two value in dictionary pythondictionary difference pythonpython compare dict keyscompare dictionary values pythonhow to compare dictionary values in python inside 1 dicctonarpython compare values inside dictdictionary comparison pythoncompare two dictionaries pythoncompare two values in a dictionary pythonget key difference comparing two dict pythonpython compare dictionary valuescomparing dictionary values pythonpython dictionary compare key to a stringpython find differences between two dictionariescompare dict pythonpython compare dictionarrieshow to compare two dictionaries with same keys in pythoncompare values of dictionary pythonpython3 compare dictionarieshow to compare two dictionaries in pythoncompare two list of dictionaries pythoncompare 2 dictionaries pythoncan we compare list and dictionary in pythonhow to compare in differnt dictionary pythonpython compare two dictionarycompare a list of dictionaries pythonhow to compare two dict in pythoncompare key values in dictionary in pythonhow to compare gensim dictionaries pythoncompare keys of two dictionaries pythoncompare state dict pytorcghpython compare two dictshow to compare values in a dict pythoncompare 2 dictionary values pythoncompare two dictionaries in python deeppy compare dictcompare values 3e in dict pythoncompare dictionarypython 3 compare dictionaries valuecompare dicts python3how to compare dictionary values in python using indexpython dictionary compare keyspython dictionaries comparecompare 2 dictionaries python an return differencelist of dict python compare valuespython compare 2 dictscompare two follow keys of two dictionaries pythonhow to compare items in dictionary pythonhow to compare each keys in a dictionary pythonpython compare two dictionaries in a list how to compare 2 dictionarycompare two dictionary keys in pythonhow to compare list and dictionary in pythoncomparing dictionaries pythonpython compare dictcompare dicts in pythonpython compare 2 dictionariespython compare variable with value of dictionarydifference between two dictionary pythonpython compare values in 2 dictpython compare keys of two dictionariescompare dict values only by keys in one dicthow to compare dictionariesdictionary compare values pythonhow to compare two dictionpython comparing nested dictionariespython3 compare keys in dictionarydifference between two dictionaries pythoncompare list of dictionaries pythonpython how to compare two dictionariespython deep compare dictionariespython diff two dictscompare dictscomparing two dictionaries in python with 3d 3dhow to compare corpora dictionaries pythonheck the difference between two dictionaries and printpython3 compare values of dictionarycomparing two dict pythonpython compare dictionary of arrayspython compares elements of both dictionariespython compare two dictionaries valueshow to compare dictionary values in pythonpython dictionary compare equaldifference between two dictionariescompare dict to dict pythondiff between two dictionaries pythonhow to get compare dictionary keys pythontwo dictionary compare in pythonhow to compare two dictionary lists in pythonhow to compare two list of dictionaries in pythoncompare two dictionaries check the same keys and same values in pythoncomparing dictionary in pythonpython compare two lists of dictionariespython compare 2 dicthow to compare tow dictionary in pythonpython compare dictionarieshow to compare dictionary elements in pythonequality between two dicts pythonpython diff between two dictshow to compare two dictionaries pythoncheck differences between two dictionaries pythoncompare two dictionaries have the same keyscompare 2 dict pythonpython dict compare 3fdictionary how does it compare a keycompare dictionaries and string in pythonpython difference of two dictionariescomparing nested dictionaries pythonhow to diff two dictionaries in pythonhow python compares dictionariespython compare list of dictionariescompare dict keyhow to compare element to keys in dictionary pythonpython compare key value several dictcomparing dictionary values in pythoncompare dictionariespython list dict comparecompare keys in dictionary pythoncompare list of dicts pythonpython compare two dictionaries and return the differencedict compare python 3compare a dictionary with a list pythoncompare dictionary in pythonhow to compare the keys of a dictionary with another valuepython compare dictonarycompare form and dictionary in pythoncompare 2 list of dictionaries pythoncompare values of 2 dictionaries pythoncompare two dictionaries python return difference compare two dicts python 3how any two dictionar compare they are samepython comparing two dictionariescompare two dict pythoncompare values of one dictionary pythonhow to compare elements in a dictionary pythonhow to compare key and value in dictionary in pythonpython compare values ib same dictionarycompare two dictionaries with nested in python and return the differencecompare values in python dictionarycompare consewunce keys of two dictionaries pythoncompare dictionaries pythoncompare values of dictionaries pythoncomparing dictionaries in pythondiff two dictionaries pythonhow to compare keys of two dictionaries in pythonpython dict compare valuescompare two dicthow to compare keys in dictionary pythondiff of 2 dictionaries python3compare values of two dictionaries pythoncompare two dictionary keys in python i a loopcompare if two dictionaries are equal pythoncompare two dictionaries keys pythoncompare list with dictionary pythonequality of dictionaries pythoncompare two nested dictionaries pythonhow to compare dictionaries in pythonhow to compare two dictionary keys and values in pythonpython dict compare 1 dict against mane dicthow python compares dictshow to compare values in dictionary pythoncompare values within dictionary pythonhow to compare python dictionariescan we compare two dictionaries in python3python3 compare two lists of dictionarieshwo to comapre two dictionaryhow to compare dict valueshow to compare values of two dictionary in pythonpython dict comparison dicttwo list of dictionary compare in pythoncomparing two dictionaries in pythonpython compare dict onlinecompare two dictionaries in pythonpython print differences between dictionariescomparing two dictionaries in python using 3d 3dhow to compare keys of the same dict in pythonhow can we compare the key values in dictionary pythoncompare two dictionary values pythonhow to compare two list of dictionaries in python using pandascompare key and value in dictionary pythoncompare two dictionaries python are same key and valuepython equal two dictionarieshow to compare 2 dictionaries in pythoncompare two dictionary keys in python in loophow to find to compare two dictioinary in pythoncompare a dictionary with many dictionary pythonpython comparing dictionaries keys and valuescompare two value in dictionarie pythonpython deep compare dictionarycompare two lists of dictionaries in pythonhow to compare values of a dictionary in pythoncompare 2 dictionaries key valueshow dict comparison works in pythonpython dictionary comprehension compare two dictionariesdictionary values compare in pythonhow to compare values in a dictionary pythonprint differences in two dicts pythoncompare values dictionary pythonpython compare two dictionaries with same keys and valuesfind difference between 2 dictionaries pythoncompare two element dictionaries pythonpython compare two dictionaries for equalitycompare dict valueshow to compare dictionary keys in pythoncan we compare 2 dictionaries in pythonlibrary for comparing two dicts pythoncompare values in dictionary pythonpython compare all elements in a dictionarycompare elements of two dictionaries pythonhow to compare dictionary in pythonpython compare two nested dictionariescompare dictionaries python 3compare dicts pythoncompare dictionary pythontell differences between dictionaries pythonpython dict comparehow to compare list with dictionary in pythondict comparison pythonpython show diff between two dictshow to compare elements of a dict in python3compare two length values in a dictionary pythonhow to compare an element with the keys in dictionary pythonpython comapre dictionary valuespython compare different dictionary valuescompare values in dict pythoncomparing dicts python3how to compare multiple dictionary as parameter in pythoncompare two dictonories pythoncompare two dictionaries in python and return the differencecompare string dictionary pythonhow to compare two dictionaries in python 3how to do comparison on all values of a dictionary pythonpython check two dictionaries are equalpython diff two dictionariespython compare 2 dicts to see if they are the samecompare values in two dictionaries pythonpython equality of dictionariescomparing two dictionaries in python with 3d 3d 3dcompare value in dictionary pythoncomparing arrays using dictionary pythoncompare dictionary keys compare two dicts python if they are identicalcompare 2 dicts pythonpython compare keys in dictionarycreate and compare a list of dictionaries pythoncompare elements in dicionary pythonpython compare list and dictionaryfind differences between two dictionaries pythonhow to compare values from two different dicts pythonhow to compare elements of a dict in pythoncompare 2 dictionaries in pythonpython compare two dictionaries keys and valueshow to compare two dicts keys in pythondifferences between two dictionaries pythondictionary comparison in pythonpython compare two mapcompare two dicts pythoncompare values of a dictionary pythonhow to compare two dictionary items in pythonpython compare two dictionariescomparing two dictionary eltcompare values in same dictionary pythonhow to compare keys dictionary pythondata in dictionary of dictionaries compare value pythontwo list with dictionaries compare pythonhow to compare to dictionaries in pythonpython comparing dictionariespython compare dict valuesdictionary compare in pythoncomparing two dictionaries in python