> ## Content Index
> Fetch the complete content index at: https://mfitzp.ghost.io/llms.txt
> Use this file to discover other available public pages before exploring further.

# Dictionary Views & Set Operations
- URL: https://mfitzp.ghost.io/python-dictionary-sets/
- Published: 2018-09-30T00:00:00.000Z
- Updated: 2024-04-02T21:06:00.000Z
- Description: Working with dictionary view objects
- Author: Martin Fitzpatrick
- Tags: Tutorials, Python, #Import 2026-08-24 14:26

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](https://blog.martinfitzpatrick.com/article/python-dictionaries?ref=mfitzp.ghost.io) 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')}

```