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'
}
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)
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)
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))
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)
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)
What will be the output?
my_dict = { 'a': 1, 'b': 2 }
print(my_dict.keys())
What will be the output?
my_dict = { 'a': 1, 'b': 2 }
print(my_dict.values())
What will be the output?
my_dict = { 'a': 1, 'b': 2 }
print(my_dict.items())
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)
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)
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)
What will be the output?
my_dict = { 'a': 'x', 'b': 'y' }
for key in my_dict:
print(key)
What will be the output?
my_dict = { 'a': 'x', 'b': 'y' }
for value in my_dict.values():
print(value)
What will be the output?
my_dict = { 'a': 'x', 'b': 'y' }
for key, value in my_dict.items():
print(key+value)