Last modified: Jan 27, 2026 By Alexander Williams
Python Compare Dictionaries: Methods & Examples
Comparing dictionaries is a common task in Python. You need to check if two dictionaries are the same. Or you might need to find differences between them.
Python offers simple and advanced ways to do this. This guide covers all the methods. You will learn about equality checks, key comparisons, and deep comparisons.
Using the Equality Operator (==)
The simplest way is using the == operator. It checks if two dictionaries have identical key-value pairs. The order does not matter in Python 3.7+.
# Example 1: Basic equality check
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 2, 'a': 1}
print(dict1 == dict2)
True
The output is True. Both dictionaries contain the same items. The == operator is the most straightforward method.
Checking for Inequality (!=)
Use the != operator to check if dictionaries are different. It returns True if any key or value does not match.
# Example 2: Inequality check
dict3 = {'x': 10, 'y': 20}
dict4 = {'x': 10, 'y': 25}
print(dict3 != dict4)
True
This is useful for validation. You can quickly see if data has changed. For more on dictionary structure, see our guide on Python Dictionary Keys.
Comparing Keys and Values Separately
Sometimes you need to compare only keys or only values. Use the .keys() and .values() methods.
# Example 3: Compare keys and values
dict_a = {'name': 'Alice', 'score': 95}
dict_b = {'name': 'Bob', 'score': 95}
# Compare keys
print(dict_a.keys() == dict_b.keys())
# Compare values
print(list(dict_a.values()) == list(dict_b.values()))
True
False
The keys are the same. The values are different. Remember, .values() returns a view. Convert it to a list for reliable comparison.
Finding Differences Between Dictionaries
To find specific differences, use set operations on dictionary keys. This shows which keys are missing or extra.
# Example 4: Find key differences
old_config = {'theme': 'dark', 'volume': 70, 'notifications': True}
new_config = {'theme': 'light', 'volume': 70, 'language': 'EN'}
# Keys in old but not in new
removed_keys = set(old_config.keys()) - set(new_config.keys())
# Keys in new but not in old
added_keys = set(new_config.keys()) - set(old_config.keys())
print("Removed:", removed_keys)
print("Added:", added_keys)
Removed: {'notifications'}
Added: {'language'}
This technique is powerful for data analysis. It helps track changes in configurations or datasets. For combining dictionaries, learn about Python Dict Merge.
Deep Comparison for Nested Dictionaries
The == operator works for nested dictionaries too. It recursively compares all inner items.
# Example 5: Compare nested dictionaries
nested_dict1 = {'user': {'id': 1, 'prefs': {'a': 1}}}
nested_dict2 = {'user': {'id': 1, 'prefs': {'a': 1}}}
print(nested_dict1 == nested_dict2)
True
For very complex structures, consider the deepdiff library. It provides detailed change reports.
Custom Comparison Logic
You can write custom functions for specific needs. For example, compare dictionaries ignoring certain keys.
# Example 6: Custom comparison ignoring a key
def compare_ignore_key(dict1, dict2, ignore_key):
# Create copies without the ignored key
d1_filtered = {k: v for k, v in dict1.items() if k != ignore_key}
d2_filtered = {k: v for k, v in dict2.items() if k != ignore_key}
return d1_filtered == d2_filtered
data1 = {'temp': 22, 'humidity': 60, 'log_id': 101}
data2 = {'temp': 22, 'humidity': 60, 'log_id': 999}
result = compare_ignore_key(data1, data2, 'log_id')
print("Dictionaries match (ignoring log_id):", result)
Dictionaries match (ignoring log_id): True
This approach is flexible. You can adapt it to many real-world scenarios. To create dictionaries dynamically, explore Python Dict Comprehension.
Common Pitfalls and Tips
Be aware of a few common issues. First, dictionary comparison is case-sensitive for keys.
Second, values must be of comparable types. Comparing a string '5' to an integer 5 returns False.
Third, always check if a dictionary is empty before comparing. An empty dict equals another empty dict. Learn more in our article Check if Python Dictionary is Empty.
Conclusion
Comparing dictionaries in Python is essential. The == operator handles most simple cases.
For more control, use set operations on keys or write custom functions. For nested data, rely on Python's built-in deep equality.
Choose the method that fits your task. Start with the equality operator. Move to advanced techniques when needed.