tutorials python

Dictionary Views & Set Operations

Working with dictionary view objects

The keys, values and items from a dictionary can be accessed using the .keys(), .values() and .items() methods. These methods return view objects which provide a view on the source dictionary.

The view objects dict_keys and dict_items support set-like operations (the latter only when all values are hashable) which can be used to combine and filter dictionary elements.

Keys

Dictionary keys are always hashable, so set operations are always available on the dict_keys view object.

All keys (set union)

To get all keys from multiple dictionaries, you can use the set union.

python
>>> d1 = {'key1':'value1', 'key2':'value2', 'key3':3}
>>> d2 = {'key3':'value3-new', 'key5':'value5'}

>>> d1.keys() | d2.keys()
{'key5', 'key3', 'key2', 'key1'}  # this is a set, not a dict

You can use the same approach to combine dict_items and merge dictionaries.

Keys in common (set intersection)

The keys common to two dictionaries can be determined using set intersection (&).

python
>>> d1 = {'key1':'value1', 'key2':'value2', 'key3':3}
>>> d2 = {'key3':'value3-new', 'key5':'value5'}

>>> d1.keys() & d2.keys()
{'key3'}    # this is a set, not a dictionary

You could use the resulting set to filter your dictionary using a dictionary comprehension.

python
>>> {k:d1[k] for k in keys}
{'key2':'value2', 'key1':'value1'}

Unique keys (set difference)

To retrieve keys unique to a given dictionary, you can use set difference (-). Keys from the right hand dict_keys are removed from the left, resulting in a set of the remaining keys.

python
>>> d1 = {'key1':'value1', 'key2':'value2', 'key3':3}
>>> d2 = {'key3':'value3-new', 'key5':'value5'}

>>> d1.keys() - d2.keys()
{'key1', 'key2'}

Unique keys from both (set symmetric difference)

If you want items unique to both dictionaries, the set symmetric difference (^) returns this. The result is items unique to both the left and right hand of the comparison.

python
>>> d1 = {'key1':'value1', 'key2':'value2', 'key3':3}
>>> d2 = {'key3':'value3-new', 'key5':'value5'}

>>> d1.keys() ^ d2.keys()
{'key5', 'key2', 'key1'}

Items

If both the keys and values of a dictionary are hashable, the dict_items view will support set-like operations.

If the values are not hashable all of these2 operations will all raise a TypeError.

Merge (set union)

You can use set union operations to merge two dictionaries.

python
>>> d1 = {'key1':'value1', 'key2':'value2', 'key3':3}
>>> d2 = {'key3':'value3-new', 'key5':'value5'}
>>> d3 = {'key4':'value4', 'key6':'value6'}

>>> d = dict(d1.items() | d2.items() | d3.items())
>>> d
{'key1':'value1', 'key2':'value2', 'key3':'value3-new', 'key5':'value5', 'key4':'value4', 'key6':'value6'}

Since it is quite common for dictionary values to not be hashable, you will probably want to use one of the other approaches for merging dictionaries instead.

Common entries (set intersection)

The items common to two dictionaries can be determined using set intersection (&). Both the key and value must match — items are compared as (key, value) tuples.

python
>>> d1 = {'key1':'value1', 'key2':'value2', 'key3':3}
>>> d2 = {'key1':'value1', 'key5':'value5'}

>>> d1.items() & d2.items()
{('key1', 'value1')}    # this is a set, not a dict

Unique entries (set difference)

To retrieve items unique to a given dictionary, you can use set difference (-). Items from the right hand dict_keys are removed from the left, resulting in a set of the remaining item (key, value) tuples.

python
>>> d1 = {'key1':'value1', 'key2':'value2', 'key3':3}
>>> d2 = {'key3':'value3-new', 'key5':'value5'}

>>> d1.items() - d2.items()
{('key3', 3), ('key2', 'value2'), ('key1', 'value1')}

Unique entries from both (set symmetric difference)

If you want items unique to both dictionaries, the set symmetric difference (^) returns this. The result is item (key, value) tuples unique to both the left and right hand of the comparison.

python
>>> d1 = {'key1':'value1', 'key2':'value2', 'key3':3}
>>> d2 = {'key3':'value3-new', 'key5':'value5'}

>>> d1.items() ^ d2.items()
{('key2', 'value2'), ('key5', 'value5'), ('key1', 'value1'), ('key3', 3), ('key3', 'value3-new')}
Over 10,000 developers have bought Create GUI Applications with Python & Qt!
Create GUI Applications with Python & Qt6
More info Get the book

Downloadable ebook (PDF, ePub) & Complete Source code

To support developers in [[ countryRegion ]] I give a [[ localizedDiscount[couponCode] ]]% discount on all books and courses.

[[ activeDiscount.description ]] I'm giving a [[ activeDiscount.discount ]]% discount on all books and courses.