Set Operations on Multiple Dictionary Keys in Python
In Python, the keys()
and items()
methods of a dictionary (dict
) enable set operations on keys and key-value pairs. For example, it is possible to create a new dictionary comprising only the common items (keys and values) across multiple dictionaries.
The keys()
and items()
methods of dict
Dictionaries (dict
) provide the keys()
and items()
methods.
The keys()
method returns a view of the keys, whereas items()
returns a view of the key-value pairs, represented as tuples (key, value)
.
d1 = {'a': 1, 'b': 2, 'c': 3}
d2 = {'b': 2, 'c': 4, 'd': 5}
print(d1.keys())
# dict_keys(['a', 'b', 'c'])
print(type(d1.keys()))
# <class 'dict_keys'>
print(d1.items())
# dict_items([('a', 1), ('b', 2), ('c', 3)])
print(type(d1.items()))
# <class 'dict_items'>
These are of types dict_keys
and dict_items
, respectively, and support set operations similar to the set
type.
Dictionaries also provide the values()
method that returns a view of values, but since values can be duplicated, set operations are not supported.
Extract common keys from multiple dictionaries: Intersection &
To extract keys common to multiple dictionaries, use the keys()
method and the &
operator.
d1 = {'a': 1, 'b': 2, 'c': 3}
d2 = {'b': 2, 'c': 4, 'd': 5}
intersection_keys = d1.keys() & d2.keys()
print(intersection_keys)
# {'c', 'b'}
The result of a set operation is a set
object, which is also true for the following examples.
print(type(intersection_keys))
# <class 'set'>
When using the items()
method, only the items with both keys and values in common are extracted. Items where only the key or only the value is common are excluded.
intersection_items = d1.items() & d2.items()
print(intersection_items)
# {('b', 2)}
You can create a new dictionary by passing the items()
method's set operation result, a (key, value)
set, directly to the dict()
constructor.
intersection_dict = dict(d1.items() & d2.items())
print(intersection_dict)
# {'b': 2}
print(type(intersection_dict))
# <class 'dict'>
Extract all keys from multiple dictionaries: Union |
To extract all keys from multiple dictionaries, use the |
operator. You can get keys present in at least one dictionary.
d1 = {'a': 1, 'b': 2, 'c': 3}
d2 = {'b': 2, 'c': 4, 'd': 5}
union_keys = d1.keys() | d2.keys()
print(union_keys)
# {'d', 'c', 'a', 'b'}
When using the items()
method, items with common keys but different values are extracted separately.
union_items = d1.items() | d2.items()
print(union_items)
# {('b', 2), ('a', 1), ('c', 4), ('d', 5), ('c', 3)}
When creating a dictionary from items with a common key but different values, only one of the values will be retained. Note that you cannot choose which value remains.
union_dict = dict(d1.items() | d2.items())
print(union_dict)
# {'b': 2, 'a': 1, 'c': 3, 'd': 5}
Starting with Python 3.9, the |
operator allows for merging two dictionaries, where in the case of overlapping keys, the values from the right-hand dictionary take precedence.
print(d1 | d2)
# {'a': 1, 'b': 2, 'c': 4, 'd': 5}
print(d2 | d1)
# {'b': 2, 'c': 3, 'd': 5, 'a': 1}
Extract unique keys from multiple dictionaries: Symmetric difference ^
To extract keys present in only one of the multiple dictionaries, use the ^
operator.
d1 = {'a': 1, 'b': 2, 'c': 3}
d2 = {'b': 2, 'c': 4, 'd': 5}
symmetric_difference_keys = d1.keys() ^ d2.keys()
print(symmetric_difference_keys)
# {'d', 'a'}
When using the items()
method, items with common keys but different values are extracted separately, as with the |
operator.
symmetric_difference_items = d1.items() ^ d2.items()
print(symmetric_difference_items)
# {('d', 5), ('a', 1), ('c', 3), ('c', 4)}
As mentioned in the previous section, when creating a dictionary from items with a common key but different values, only one of the values will be retained. The selection of which value remains cannot be specified.
symmetric_difference_dict = dict(d1.items() ^ d2.items())
print(symmetric_difference_dict)
# {'d': 5, 'a': 1, 'c': 4}
You can also use the -
operator to get the set difference.
difference_keys = d1.keys() - d2.keys()
print(difference_keys)
# {'a'}
difference_items = d1.items() - d2.items()
print(difference_items)
# {('a', 1), ('c', 3)}
difference_dict = dict(d1.items() - d2.items())
print(difference_dict)
# {'a': 1, 'c': 3}