Looking things up by name
After this: Make a dictionary and read a value by its key, with a fallback for a key that is not there.
On this page · 6 sections · 3 checks
You keep a phone list on paper: a name, and next to it a number. When you want to call Ben, you do not count down the page to the fourth line. You look for the name Ben and read the number beside it.
A list from the Lists tutorial cannot do that. A list is read by position: phones[3]phones.get(3) means "the fourth value", and to find Ben you would have to go through the whole list and check each name. What you want is a collection that is read by name.
Pairs: a key and a value
Write the phone list as pairs: Ana with 555-0131, Ben with 555-0142. Each pair has two halves. The half you look things up by is the , and the half that comes back is the . One key together with its value is a . A is a collection of pairs: you give it a key, and it gives you the value.
The name fits. In a real dictionary you look up a word and read its meaning, and nobody counts the pages to get there.
The smallest example, by hand
Take two pairs of ages: Ana with 31 and Ben with 25.
- Ask for the key Ana. The dictionary finds the pair whose key is Ana and gives back its value: 31.
- Ask for the key Ben: 25.
- Ask how many pairs there are: two.
The rule: a key goes in, a value comes back. Each key appears once, so there is never a question of which Ana you mean. Keys are often text, as here, but a number can be a key too.
The first dictionary in code
Here is the phone list as a program. It makes the dictionary, reads two values by key, and counts the pairs. Press Run it and step through.
- Run the programRun it and step through. The box phones holds every pair. Watch lines 2 and 3 (Java: 7 and 8) pick one value out by its key, and line 4 (Java: line 9) count the pairs.
phones = {"Ana": "555-0131", "Ben": "555-0142"}print(phones["Ana"])print(phones["Ben"])print(len(phones))run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
- Line 16 makes the dictionary with two pairs and puts it in the box
phones. The band draws the whole box: each key, a colon, its value. - Line 27 reads by key: phones["Ana"]phones.get("Ana") gives the key Ana and gets 555-0131 back. The print shows it.
- Line 49 asks for the length: len(phones)phones.size() is 2, the number of pairs.
Python runs in the browser when you press Run on the block below. Java does not run here: if you are learning Java, step through the trace above instead, and paste the code into a Java tool on your computer when you want to run it for real.
Add a third pair with your own name and number, print it by its key, and run again.
A key that is not there
What if you ask for Cy, and there is no Cy? The dictionary has no value to give. So every dictionary has a second way to read, made for this case: you give the key and a fallback value, and the fallback comes back when the key is not there. ages.get("Cy", 0)ages.getOrDefault("Cy", 0) gives 0 for a missing Cy. ages.get("Ben", 0)ages.getOrDefault("Ben", 0) gives Ben's own value, 25, because Ben is there; the fallback is used only when the key is missing.
ages = {"Ana": 31, "Ben": 25}print(ages.get("Ben", 0))print(ages.get("Cy", 0))run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Without a fallback, get gives back the marker for "nothing", Nonenull:
ages = {"Ana": 31, "Ben": 25}print(ages.get("Cy"))None
HashMap<String, Integer> ages = new HashMap<>(Map.of("Ana", 31, "Ben", 25));System.out.println(ages.get("Cy"));null
What the plain read does with a key that is not there is in the next lesson, because the two languages do not agree.
Check yourself
The dictionary ages holds the pairs Ana: 31 and Ben: 25. What is ages["Ana"]ages.get("Ana")?
The second value in each read is the fallback. What does this print?
stock = {"apples": 4, "pears": 0}print(stock.get("pears", 9))print(stock.get("plums", 9))HashMap<String, Integer> stock = new HashMap<>(Map.of("apples", 4, "pears", 0));System.out.println(stock.getOrDefault("pears", 9));System.out.println(stock.getOrDefault("plums", 9));Complete the missing line so that the box age holds Ana's age and the program prints 31.
ages = {"Ana": 31, "Ben": 25}____________________ # ← put Ana's age in the box ageprint(age)HashMap<String, Integer> ages = new HashMap<>(Map.of("Ana", 31, "Ben", 25));____________________ // ← put Ana's age in the box ageSystem.out.println(age);Honesty notes
- Say what a dictionary is: pairs of a key and a value, where you read a value by giving its key.
- Make a dictionary (curly brackets in Python, new HashMap in Java) and read one value by its key.
- Ask how many pairs a dictionary holds (len in Python, size in Java).
- Read a key that may not be there with a fallback value (get with a second value in Python, getOrDefault in Java).