note.nkmk.me

Set operations on multiple dictionary keys in Python

Posted: 2021-09-11 / Tags: Python, Dictionary

In Python, the keys() and items() methods of dictionaries dict can be used to perform set operations on keys and key-value pairs. For example, you can generate a dictionary consisting of elements (keys and values) that are common to multiple dictionaries.

This article describes the following contents.

  • The keys() and items() methods of dict
  • Extract keys common to multiple dictionaries: Intersection &
  • Extract all keys from multiple dictionaries: Union |
  • Extract keys that are contained in only one of multiple dictionaries: Symmetric difference ^

Use the following two dictionaries as examples.

d1 = {'a': 1, 'b': 2, 'c': 3}
d2 = {'b': 2, 'c': 4, 'd': 5}
Sponsored Link

The keys() and items() methods of dict

The dictionary has keys() and items() methods.

keys() returns the view of keys, items() returns the view of key-value tuples (key, value).

print(list(d1.keys()))
# ['a', 'b', 'c']

print(type(d1.keys()))
# <class 'dict_keys'>

print(list(d1.items()))
# [('a', 1), ('b', 2), ('c', 3)]

print(type(d1.items()))
# <class 'dict_items'>

They are dict_keys and dict_items respectively, and support set operations like set.

The dictionary also has a values method that returns a view of values, but set operations are not supported because values may overlap.

In the following, examples of set operations using the keys() and items() methods are shown.

Extract keys common to multiple dictionaries: Intersection &

Keys that are common to multiple dictionaries can be extracted with the keys() method and the & operator.

intersection_keys = d1.keys() & d2.keys()
print(intersection_keys)
# {'c', 'b'}

The result of a set operation is of type set. The same applies to the following examples.

print(type(intersection_keys))
# <class 'set'>

In the case of items(), items with both keys and values in common are extracted. Items that have only the key or only the value in common are excluded.

intersection_items = d1.items() & d2.items()
print(intersection_items)
# {('b', 2)}

You can generate new dictionary by passing a set of (key, value) tuples (= the result of the set operation of items()) to dict().

intersection_dict = dict(d1.items() & d2.items())
print(intersection_dict)
# {'b': 2}

print(type(intersection_dict))
# <class 'dict'>
Sponsored Link

Extract all keys from multiple dictionaries: Union |

All keys in multiple dictionaries, that is, keys that are contained in at least one of the multiple dictionaries, can be extracted with the | operator.

union_keys = d1.keys() | d2.keys()
print(union_keys)
# {'d', 'a', 'b', 'c'}

In the case of items(), elements with common keys but different values are extracted separately.

union_items = d1.items() | d2.items()
print(union_items)
# {('d', 5), ('c', 4), ('a', 1), ('b', 2), ('c', 3)}

As in this example, if there are elements with a common key but different values, only one of them is retained when generating a dictionary from the set. It is not possible to specify which value is to be retained.

union_dict = dict(d1.items() | d2.items())
print(union_dict)
# {'d': 5, 'c': 3, 'a': 1, 'b': 2}

Extract keys that are contained in only one of multiple dictionaries: Symmetric difference ^

Keys that are contained in only one of the multiple dictionaries can be extracted with the ^ operator.

symmetric_difference_keys = d1.keys() ^ d2.keys()
print(symmetric_difference_keys)
# {'d', 'a'}

In the case of items(), as with the | operator, elements with common keys but different values are extracted separately.

symmetric_difference_items = d1.items() ^ d2.items()
print(symmetric_difference_items)
# {('d', 5), ('c', 4), ('a', 1), ('c', 3)}

As in this example, if there are elements with a common key but different values, only one of them is retained when generating a dictionary from the set. It is not possible to specify which value is to be retained.

symmetric_difference_dict = dict(d1.items() ^ d2.items())
print(symmetric_difference_dict)
# {'d': 5, 'c': 3, 'a': 1}

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)
# {('c', 3), ('a', 1)}

difference_dict = dict(d1.items() - d2.items())
print(difference_dict)
# {'c': 3, 'a': 1}
Sponsored Link
Share

Related Categories

Related Articles