Dictionaries can be used as a static one-to-one map between keys and values. For example, a rough (and rather poor) translation from English to Finnish could be stored in a dictionary (as we have started to do in Code Example 5.9). Then to translate a sentence like `social science needs programming skills', we would go through the dictionary and use the Finnish counterpart in our new sentence. (The outcome would be: `sosiaalinen tiede tarvitsee ohjelmointi taidot', which is far from a perfect translation, but it makes sense and communicates the intended meaning.) While this is an artificial example, there are many legitimate cases and interests to use dictionaries to store values that we do not intend to change during the execution of the project.
However, dictionaries have a problem: If the key is not found in a dictionary, the program does not work as intended. Let us imagine a survey where participants have been asked to enter their zip code, and we wish to translate those into city names. This is highly static information. Each zip code has one city that we could easily list. In the United States, 14853 maps to Ithaca, 94305 to Palo Alto and 27708 to Durham. Using a dictionary, we can easily translate zip codes into textual data. There are keys (zip codes), and they have corresponding values (city names). However, a survey responder might have made a mistake and entered a non-existing zip code into the survey. This would lead to major errors! Therefore, we want to first check if the key is available in the dictionary and, if it is, translate the key into a corresponding value. However, if the key does not exist in the dictionary, we must somehow handle the error. Code Example 5.10 illustrates one approach to managing these, labelling all wrongly labelled zip codes as `Error'.
The static values are not the only use - and not the most meaningful use - for dictionaries. Dictionaries serve as a mega-variable. When there is more than one thing to account for, dictionaries enable managing this. That is, we can use dictionaries when we have several different types of units that we seek to find the most wanted through a most-wanted holder, find a sum or aggregate value through a gatherer, identify their existence using a flag or use a follower approach for programs. However, we must use the variable indicating the type as a key in our dictionary. The variable value is used as the key to which bucket in the dictionary the value is stored. Code Examples 5.11 and 5.12 show how to use dictionaries as mega-variables. (Note that the code may require checking that the keys exist in the dictionary as we did in Code Example 5.10). For example, the degree calculation example (see Code Example 5.8) uses a dictionary as a mega-gatherer.
Using dictionaries in this manner allows simplification of the problem. In many cases, lists and dictionaries help avoid copying and pasting code lines. Code Examples 5.11 and 5.12 could be done by creating separate variables to each case and managing each case with its own if-structure. However, this would require us to know about all the cases before and copy-paste a similar kind of code for each case. Finally, dictionaries allow accessing keys and values as lists. Thus, we can easily transform a dictionary to a list. This can help in some cases where it is easier to analyse the data as a list (e.g. when computing an average of numerical values).