compare_dicts

pyhelpers.ops.compare_dicts(dict1, dict2)[source]

Compare the difference between two dictionaries.

See also [OPS-CD-1].

Parameters:
  • dict1 (dict) – a dictionary

  • dict2 (dict) – another dictionary

Returns:

in comparison to dict1, the main difference on dict2, including: modified items, keys that are the same, keys where values remain unchanged, new keys and keys that are removed

Return type:

Tuple[dict, list]

Examples:

>>> from pyhelpers.ops import compare_dicts

>>> d1 = {'a': 1, 'b': 2, 'c': 3}
>>> d2 = {'b': 2, 'c': 4, 'd': [5, 6]}

>>> items_modified, k_shared, k_unchanged, k_new, k_removed = compare_dicts(d1, d2)
>>> items_modified
{'c': [3, 4]}
>>> k_shared
['b', 'c']
>>> k_unchanged
['b']
>>> k_new
['d']
>>> k_removed
['a']