To compare two dictionaries in Python and check how many keys and values are common, different, or missing, you can use set operations or iterate over the dictionaries.
Here’s a breakdown of what you might be asking for:
1. Comparing Keys and Values Between Two Dictionaries
To compare two dictionaries, you can do things like:
- Find common keys
- Find keys that are only in one dictionary but not the other
- Compare values for matching keys
Example of Comparing Two Dictionaries:
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'b': 2, 'c': 3, 'd': 4}
# Common keys (keys that exist in both dictionaries)
common_keys = dict1.keys() & dict2.keys()
# Keys only in dict1
only_in_dict1 = dict1.keys() - dict2.keys()
# Keys only in dict2
only_in_dict2 = dict2.keys() - dict1.keys()
# Common key-value pairs (keys with matching values)
common_key_value = {k: dict1[k] for k in common_keys if dict1[k] == dict2[k]}
# Different key-value pairs (keys where values are different)
different_key_value = {k: (dict1[k], dict2[k]) for k in common_keys if dict1[k] != dict2[k]}
print("Common keys:", common_keys)
print("Only in dict1:", only_in_dict1)
print("Only in dict2:", only_in_dict2)
print("Common key-value pairs:", common_key_value)
print("Different key-value pairs:", different_key_value)
Output:
Common keys: {'b', 'c'}
Only in dict1: {'a'}
Only in dict2: {'d'}
Common key-value pairs: {'b': 2, 'c': 3}
Different key-value pairs: {}
2. How Many Common Key-Value Pairs?
If you just want to check how many key-value pairs are common and match in both dictionaries, you can simply count them:
common_count = len(common_key_value)
print("Number of common key-value pairs:", common_count)
3. How Many Keys Differ?
To check how many keys are different (i.e., they exist in one dictionary but not the other):
only_in_dict1_count = len(only_in_dict1)
only_in_dict2_count = len(only_in_dict2)
print("Keys only in dict1:", only_in_dict1_count)
print("Keys only in dict2:", only_in_dict2_count)
4. How Many Key-Value Pairs Have Different Values?
To count how many keys have different values:
different_count = len(different_key_value)
print("Number of different key-value pairs:", different_count)
5. Full Example with Counts
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'b': 2, 'c': 3, 'd': 4}
common_keys = dict1.keys() & dict2.keys()
only_in_dict1 = dict1.keys() - dict2.keys()
only_in_dict2 = dict2.keys() - dict1.keys()
common_key_value = {k: dict1[k] for k in common_keys if dict1[k] == dict2[k]}
different_key_value = {k: (dict1[k], dict2[k]) for k in common_keys if dict1[k] != dict2[k]}
# Counts
common_count = len(common_key_value)
only_in_dict1_count = len(only_in_dict1)
only_in_dict2_count = len(only_in_dict2)
different_count = len(different_key_value)
print("Common keys:", common_keys)
print("Keys only in dict1:", only_in_dict1_count)
print("Keys only in dict2:", only_in_dict2_count)
print("Common key-value pairs:", common_key_value)
print("Different key-value pairs:", different_key_value)
print(f"Number of common key-value pairs: {common_count}")
print(f"Number of different key-value pairs: {different_count}")
Output:
Common keys: {'b', 'c'}
Keys only in dict1: 1
Keys only in dict2: 1
Common key-value pairs: {'b': 2, 'c': 3}
Different key-value pairs: {}
Number of common key-value pairs: 2
Number of different key-value pairs: 0