Iterating with Dictionaries
Many times in this book, we looped through the characters in a string or the elements in a list without using the index values.
|
|
Each time the loop body repeats, the loop variable (character
or
number
) gets assigned the next value in the collection.
We can do something similar with dictionaries.
Loop by Keys or Values
To loop through a dictionary, we need to specify whether to assign the loop
variable the keys or the values from the collection. To do this, we use the
keys()
or values()
method in the for
statement:
|
|
In the first for
loop, each time the code repeats, the loop variable
key
gets assigned one of the key names from the dictionary. In the second
loop, the variable value
gets assigned a new value each iteration.
We do not have to use the name key
or value
for the loop variable,
but doing so helps keep our code clear.
Loop by Key/Value Pairs
The items()
method returns each key/value pair as a unit, and it allows us
to assign BOTH the key and value from the dictionary to separate variables. The
general syntax for this is:
for (key, value) in dictionary_name.items():
In the for
statement, we define a pair of variables (key, value)
to
hold a key name and its linked value from the dictionary. Each iteration, these
two variables represent a new key/value pair from the collection.
Compare the following two loops, which do exactly the same thing:
|
|
By defining a pair of variables, we can access the values from the
dictionary without needing to use bracket notation. On line 14, the variable
value
replaces comics[key]
in our code.
Sorting by Keys
Dictionaries are unordered collections, so they do NOT include any type of sorting method. However, sometimes we might want to access or display the key/value pairs in a particular order—like alphabetically by key name.
If we want to sort a dictionary, the short answer is…we can’t. However, we can use a work-around.
By adding copies of the key names to a separate list, we can sort the list to get the order we want. Then we use the key names from the sorted list to access the values in the dictionary.
Check Your Understanding
Given the code:
|
|
What is the value of comics[key]
the third time through the loop?
'Wiley Miller'
'Bill Watterson'
'Non Sequitur'
'Calvin and Hobbs'