Dictionaries

Object views and iterating over dictionaries

In this Python lesson, you'll explore object views, dynamic representations of a dictionary's keys, values, or items, and learn how to use them to iterate over dictionaries.


Let's create a new dictionary that we can work with:

capitals = { 'USA': 'Washington D.C.', 'France': 'Paris', 'Brazil': 'Brasília', 'Peru': 'Lima' }
Python

Sometimes, you'll need to extract the keys from a dictionary.

You can do so using the dict.keys() method.

capitals = { 'USA': 'Washington D.C.', 'France': 'Paris', 'Brazil': 'Brasília', 'Peru': 'Lima' } keys = capitals.keys() print(keys)
Python
Output

The dict.keys() method returns a so-called view object.

This view object provides a dynamic view of the dictionary's keys.

It's dynamic because it reflects changes made to the dictionary in real-time.

Here we add a new key after creating the view object.

capitals = { 'USA': 'Washington D.C.', 'France': 'Paris', 'Brazil': 'Brasília', 'Peru': 'Lima' } keys = capitals.keys() print(keys) # add new key-value pair capitals['Germany'] = 'Berlin' print(keys)
Python
Output

Notice that the new key has been added to the view object.

You can transform the view object into a static list using the list() function:

capitals = { 'USA': 'Washington D.C.', 'France': 'Paris', 'Brazil': 'Brasília', 'Peru': 'Lima' } keys = capitals.keys() # transform view object into list print(list(keys))
Python
Output

If you want to extract a dictionary's values instead of its keys, you can use the dict.values() method:

capitals = { 'USA': 'Washington D.C.', 'France': 'Paris', 'Brazil': 'Brasília', 'Peru': 'Lima' } values = capitals.values() print(values)
Python
Output

Another view object method is dict.items(), which returns a dynamic view of all key-value pairs in a dictionary:

capitals = { 'USA': 'Washington D.C.', 'France': 'Paris', 'Brazil': 'Brasília', 'Peru': 'Lima' } items = capitals.items() print(items)
Python
Output

What will be the output?

my_dict = { 'a': 1, 'b': 2 } print(my_dict.keys())
Python

What will be the output?

my_dict = { 'a': 1, 'b': 2 } print(my_dict.values())
Python

What will be the output?

my_dict = { 'a': 1, 'b': 2 } print(my_dict.items())
Python

Sometimes it's necessary to iterate over keys, values or key-value pairs in dictionaries.

We can accomplish this using a for-loop and the view objects introduced earlier.

Here, we iterate over the values of our dictionary:

capitals = { 'USA': 'Washington D.C.', 'France': 'Paris', 'Brazil': 'Brasília', 'Peru': 'Lima' } # loop over values for value in capitals.values(): print(value)
Python
Output

We don't need to use a view object to iterate over the keys of our dictionary:

capitals = { 'USA': 'Washington D.C.', 'France': 'Paris', 'Brazil': 'Brasília', 'Peru': 'Lima' } # loop over keys for key in capitals: print(key)
Python
Output

With dict.items() we can iterate over keys and values simultaneously:

capitals = { 'USA': 'Washington D.C.', 'France': 'Paris', 'Brazil': 'Brasília', 'Peru': 'Lima' } # loop over key-value pairs for key, value in capitals.items(): print('The capital of', key, 'is', value)
Python
Output

What will be the output?

my_dict = { 'a': 'x', 'b': 'y' } for key in my_dict: print(key)
Python

What will be the output?

my_dict = { 'a': 'x', 'b': 'y' } for value in my_dict.values(): print(value)
Python

What will be the output?

my_dict = { 'a': 'x', 'b': 'y' } for key, value in my_dict.items(): print(key+value)
Python