Python: How to find the difference between 2 dictionaries

Updated: February 12, 2024 By: Guest Contributor Post a comment

Introduction

In Python, dictionaries are crucial data structures used to store collections of items, each with a key and a corresponding value. Understanding how to compare these structures, specifically finding differences between two dictionaries, is vital in diverse applications ranging from data analysis to configuration management. This article will guide you through various methods to uncover these differences, offering insights from basic to advanced examples.

Basic Comparison

To start, it’s key to grasp the foundational ways of comparing dictionaries. Imagine we have two dictionaries representing the inventory of two different stores:

store_a = {'apples': 30, 'bananas': 15, 'grapes': 40}
store_b = {'apples': 35, 'bananas': 15, 'oranges': 20}

The simplest form of comparison involves identifying keys and values that differ. One way to achieve this is by using dictionary comprehension together with comparison operators:

keys_a = store_a.keys()
keys_b = store_b.keys()

diff_keys_in_a = {k: store_a[k] for k in keys_a if k not in keys_b}
diff_keys_in_b = {k: store_b[k] for k in keys_b if k not in keys_a}
common_keys = keys_a & keys_b
diff_values = {k: (store_a[k], store_b[k]) for k in common_keys if store_a[k] != store_b[k]}

This approach helps us identify unique keys in each dictionary and the different values for the keys present in both. The output would be:

{'grapes': 40}, {'oranges': 20}, {'apples': (30, 35)}

Using External Libraries

For more sophisticated scenarios, external libraries like deepdiff offer expanded capabilities. Following the installation of deepdiff, you can easily spot detailed differences, including items added, removed, or altered in complex nested dictionaries:

from deepdiff import DeepDiff

store_c = {
    'apples': 30,
    'bananas': {'green': 24, 'yellow': 18, 'brown': 0},
    'grapes': 'none'
}

store_d = {
    'apples': 30,
    'bananas': {'green': 5, 'yellow': 15, 'brown': 1},
    'oranges': 20,
    'wing': {'candidates': {'graduates': 'expanded', 'professionals': 'revised'}},
    'melian': {'locations': {'North America': 'updated', 'Europe': 'unchanged'}}
}

diff = DeepDiff(store_c, store_d)
print(diff)

This script computes a deeply nested dictionary difference, allowing developers to see exactly what changes occurred. For our example dictionaries, the output would be a comprehensive account of differences, revealing both high-level and deeply-nested discrepancies.

Advanced Structural Comparison

For users requiring an even deeper comparison, particularly when dealing with complex, nested dictionaries and wishing to maintain full control over the process, Python’s inherent abilities can be pushed further. By combining recursive functions with dictionary comprehension, you can tailor a solution to precisely meet your needs. This approach affords scrutiny over each comparison’s granularity and is ideal for complex cases where external libraries might not offer the required flexibility or detail:

def deep_diff(dict_a, dict_b, level='root'):
    diffs = {}
    for k in dict_a.keys() | dict_b.keys():
        if k not in dict_a or k not in dict_b:
            diffs[(level, k)] = 'Key exists in one dict only'
        elif isinstance(dict_a[k], dict) and isinstance(dict_b[k], dict):
            deeper_diffs = deep_diff(dict_a[k], dict_b[k], level=f'{level}.{k}')
            if deeper_diffs:
                diffs.update(deeper_diffs)
        else if dict_a[k] != dict_b[k]:
            diffs[(level, k)] = f'Values differ: {dict_a[k]} vs {dict_b[k]}'
    return diffs

# Using this function on deeply nested dictionaries will unravel layers of differences, offering precise insights into what exactly changes between the two structures.

Conclusion

Mastering the comparison of Python dictionaries from basic key-value checks to deep, algorithm-driven insights paves the way for better data management and understanding. Whether your needs are simple or complex, Python provides the tools to efficiently compare dictionaries, highlighted by the methods discussed. These examples, ranging from straightforward to advanced, illustrate the versatility and power of Python for data analysis and manipulation, ultimately broadening your programming repertoire.