Today we’ll be talking about the dictionary view. Actually, there are three of them, the keys view, the values view and the items view. These are all iterables, so they return one item at a time.
Here’s the video version:
We can iterate over the elements contained in the views manually, by using the built-in next function, or automatically in a for loop.
In order to obtain a keys view, a values view or an items view, we use the keys, values and items methods respectively:
>>> first_editions = {"Who Ate My Dinner?": 1952,
... "They All Knew": 1987,
... "The Woman Who Didn't Breathe": 2001,
... "The Tragedy on the Sea": 1921}
>>> kview = first_editions.keys()
>>> vview = first_editions.values()
>>> iview = first_editions.items()
Let’s check out whether these are really views:
>>> type(kview), type(vview), type(iview)
(<class 'dict_keys'>, <class 'dict_values'>, <class 'dict_items'>)
Manual Iteration over a Dictionary View
A dictionary view is an iterable, but not an iterator, so we have to use the iter function to obtain its iterator. Then we can use the next method to iterate over the elements one by one manually.
If you don’t understand the difference between an iterable and an iterator, make sure to read this article.
Anyway, here’s how it works. At first we’ll create three iterators, one from each view. Then we will be iterating over the elements of the keys view and over the elements of the other two views one by one. To this end we’ll be using the next method. When each of the iterators gets exhausted, the StopIteration error will be raised.
>>> itK = iter(kview)
>>> itV = iter(vview)
>>> itI = iter(iview)
>>>
>>> next(itK)
'Who Ate My Dinner?'
>>> next(itK)
'They All Knew'
>>> next(itK)
"The Woman Who Didn't Breathe"
>>> next(itK)
'The Tragedy on the Sea'
>>> next(itK)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>> next(itV)
1952
>>> next(itV)
1987
>>> next(itV)
2001
>>> next(itV)
1921
>>> next(itV)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>> next(itI)
('Who Ate My Dinner?', 1952)
>>> next(itI)
('They All Knew', 1987)
>>> next(itI)
("The Woman Who Didn't Breathe", 2001)
>>> next(itI)
('The Tragedy on the Sea', 1921)
>>> next(itI)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
Iterating in a Loop
The example above is good for the purpose of demonstrating how iterators work. But usually, we don’t iterate like this. Instead we use the for loop, which automatically obtains the iterator from the view and iterates over all the elements in it. We’re going to rewrite the code from the example now so that it makes use of loops to iterate over the elements from the views:
>>> for k in kview:
... print(k)
...
Who Ate My Dinner?
They All Knew
The Woman Who Didn't Breathe
The Tragedy on the Sea
>>> for v in vview:
... print(v)
...
1952
1987
2001
1921
>>> for i in iview:
... print(i)
...
('Who Ate My Dinner?', 1952)
('They All Knew', 1987)
("The Woman Who Didn't Breathe", 2001)
('The Tragedy on the Sea', 1921)
In many cases we don’t need the views at all, but rather just the elements. If we want to retrieve all the elements immediately, we can use the list function:
>>> list(kview)
['Who Ate My Dinner?', 'They All Knew', "The Woman Who Didn't Breathe", 'The Tragedy on the Sea']
>>> list(vview)
[1952, 1987, 2001, 1921]
>>> list(iview)
[('Who Ate My Dinner?', 1952), ('They All Knew', 1987), ("The Woman Who Didn't Breathe", 2001), ('The Tragedy on the Sea', 1921)]