Show the three Python dictionary views in use and how to convert them into lists.
A dictionary gives you three different views of its contents. There is one view for the keys, one for the values, and one for the key and value pairs together.
scores = {'ada': 99, 'alan': 91}
print(list(scores.keys())) # ['ada', 'alan']
print(list(scores.values())) # [99, 91]
print(list(scores.items())) # [('ada', 99), ('alan', 91)]
# A view is live, so it shows later changes.
view = scores.keys()
scores['grace'] = 87
print(list(view)) # ['ada', 'alan', 'grace']
Each of these methods returns a view object and not a list, and the last block shows the difference that matters. The view was taken before Grace was added, and yet listing it afterwards includes her. A view is a window onto the dictionary, and it is not a photograph of the dictionary at one moment.
That behaviour is useful, because you can hold on to a view and keep iterating it. It also has a consequence that catches people out. If you change the number of items in the dictionary while you are iterating over a view, Python raises a RuntimeError that tells you the dictionary changed size during iteration. Assigning to a key that already exists is fine, because the size stays the same, but adding or removing a key is not fine.
Wrapping a view in list() takes a snapshot of it, and that is what you want when you need to iterate over the keys and change the dictionary inside the same loop.
Another detail worth knowing is that the values view is not unique. Two different keys can hold the same value, so the list of values can contain duplicates even though the list of keys never can. If you want the distinct values, put them in a set.