Similar to lists, dictionaries can store several values in one variable. However, unlike lists, dictionaries are not automatically indexed. Rather, the index is manually defined by the programmer. All values in lists are combinations of keys (that define the index location) and corresponding values for each key (see Figure 5.3). Lists provide a fast and easy way to access the values based on keys.
When we were computing the degree of node C in Code Example 4.1, we could only do this for one node at a time. We did this by defining a gatherer variable to compute the degree for the node. Dictionaries allow us to easily create gatherer variables for each node (see Code Example 5.8). We create a dictionary for degrees and use the node names as keys. As in the beginning, all degree values are zero, and we can define them as zero as well. Every time we process a node, we add the degree of that node by one. We use the name of the node as a key and store the increased degree as a value.