Answers for "difference between two dictionaries python"

3

compare two dictionaries in python

def dict_compare(d1, d2):
    d1_keys = set(d1.keys())
    d2_keys = set(d2.keys())
    shared_keys = d1_keys.intersection(d2_keys)
    added = d1_keys - d2_keys
    removed = d2_keys - d1_keys
    modified = {o : (d1[o], d2[o]) for o in shared_keys if d1[o] != d2[o]}
    same = set(o for o in shared_keys if d1[o] == d2[o])
    return added, removed, modified, same

x = dict(a=1, b=2)
y = dict(a=2, b=2)
added, removed, modified, same = dict_compare(x, y)
Posted by: Guest on April-04-2021
0

difference between two dictionaries python

value = { k : second_dict[k] for k in set(second_dict) - set(first_dict) }
Posted by: Guest on September-24-2021
0

comparing two dictionaries in python

In [1]: from deepdiff import DeepDiff

In [2]: a = {
   ...:     'number': 1,
   ...:     'list': ['one', 'two']
   ...: }

In [3]: b = {
   ...:     'list': ['one', 'two'],
   ...:     'number': 2
   ...: }

In [4]: diff = DeepDiff(a, b)

In [5]: diff
Out[5]: {'values_changed': {"root['number']": {'new_value': 2, 'old_value': 1}}}
Posted by: Guest on September-18-2021

Code answers related to "difference between two dictionaries python"

Python Answers by Framework

Browse Popular Code Answers by Language