compare_dicts

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

Compare the differences between two dictionaries.

See also [OPS-CD-1].

Parameters:
  • dict1 (dict) – The first dictionary for comparison.

  • dict2 (dict) – The second dictionary for comparison.

Returns:

A tuple containing the main differences between dict1 and dict2: modified items, common keys with different values, unchanged keys (same value in both dictionaries), new keys added in dict2 and keys removed from dict1.

Return type:

tuple

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']