Adding, changing, removing
After this: Put a pair in, change a value, take a pair out, and say what a missing key does.
On this page · 7 sections · 3 checks
Ages change every year. A new person joins the list, and someone leaves it. In Lists you changed a list with put in, take out and replace, and watched the neighbours move. A dictionary changes too, and the moves are fewer, because a dictionary has no positions and nothing has to shift.
Three changes
| Change | Python | Java | What happens |
|---|---|---|---|
| Add a pair | ages["Cy"] = 40 | ages.put("Cy", 40) | a new key gets a value: one more pair |
| Change a value | ages["Ben"] = 26 | ages.put("Ben", 26) | an old key gets a new value: the same pairs |
| Take a pair out | del ages["Ana"] | ages.remove("Ana") | the key and its value go: one pair fewer |
The first two are the same line. Giving a key a value either adds a pair, when the key is new, or changes the pair, when the key is already there. The dictionary decides which by looking for the key. There is no "put it at position 2": a new pair goes in beside the others.
By hand
Start with Ana: 31 and Ben: 25.
- Give Cy the value 40. Cy is not a key, so a pair is added. Three pairs.
- Give Ben the value 26. Ben is a key, so his value changes from 25 to 26. Still three pairs.
- Take out Ana. The pair Ana: 31 goes. Two pairs: Ben: 26 and Cy: 40.
The same changes in code
Each change is followed by a print: the number of pairs after the add, Ben's value after the change, and the number of pairs after the removal.
- Run the programRun it and watch the box ages. It gains a pair on line 2, keeps the same keys but a new value on line 4, and loses a pair on line 6 (Java: lines 7, 9 and 11).
ages = {"Ana": 31, "Ben": 25}ages["Cy"] = 40print(len(ages))ages["Ben"] = 26print(ages["Ben"])del ages["Ana"]print(len(ages))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
Change line 2 so that it gives Ben a value instead of Cy, and run again. How many pairs are there now?
Asking first
Before you read a key that may not be there, you can ask. "Cy" in agesages.containsKey("Cy") is Truetrue when Cy is a key and Falsefalse when it is not.
ages = {"Ana": 31, "Ben": 25}if "Cy" in ages: print(ages["Cy"])else: print("no Cy")no Cy
HashMap<String, Integer> ages = new HashMap<>(Map.of("Ana", 31, "Ben", 25));if (ages.containsKey("Cy")) { System.out.println(ages.get("Cy"));} else { System.out.println("no Cy");}no Cy
The next lesson asks this question for every word of a text.
Reading a key that is not there
This is the one place in the tutorial where the two languages part ways. The program below reads the key Cy with the plain read, and Cy is not there. Step to line 27 and watch what happens.
ages = {"Ana": 31, "Ben": 25}print(ages["Cy"])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
Check yourself
The dictionary ages starts with the pairs Ana: 31 and Ben: 25. Then ages["Cy"] = 40ages.put("Cy", 40) runs, and then ages["Ben"] = 26ages.put("Ben", 26). Which pairs does it hold now? The options are written the way the trace band draws a dictionary.
Three changes, then two prints. What does this print?
stock = {"apples": 4}stock["pears"] = 2stock["apples"] = 5del stock["pears"]print(stock["apples"])print(len(stock))HashMap<String, Integer> stock = new HashMap<>(Map.of("apples", 4));stock.put("pears", 2);stock.put("apples", 5);stock.remove("pears");System.out.println(stock.get("apples"));System.out.println(stock.size());Cy is not a key. What happens on line 2?
ages = {"Ana": 31, "Ben": 25}print(ages["Cy"])KeyError: 'Cy'
HashMap<String, Integer> ages = new HashMap<>(Map.of("Ana", 31, "Ben", 25));System.out.println(ages.get("Cy"));null
Honesty notes
- Add a pair by giving a new key a value, and change a value by giving an old key a new one.
- Take a pair out by its key (del in Python, remove in Java).
- Ask whether a key is there before reading it (in for Python, containsKey in Java).
- Say what reading a missing key does: Python stops the program, Java gives back null.