What this topic tests
- Read a value safely with get instead of crashing on a missing key.
- Update, merge, and iterate over keys, values, or pairs.
- Recognise which objects can be used as dictionary keys.
Common mistakes
- Using d[key] when the key may be absent. Decide whether get or a default is more appropriate.
- Changing the dictionary size while iterating over it. Iterate over a list of keys or items instead.
- Using a list as a key. Dictionary keys must be hashable.
Sample questions
Ten representative questions from the full bank. Try each one before opening the answer.
-
1. Ming looks up a price with menu.get("fruit tea", "not available"). What is returned if fruit tea is not on the menu?
Show answer
Answer
B. "not available" (when the key is absent)Why: get(key, default) returns the stored value when the key exists and the default when it does not; it never inserts. So a missing fruit tea yields 'not available'.
-
2. What does d.pop('a') raise when the key is missing and no default is given?
Show answer
Answer
A. KeyErrorWhy: pop removes a key and returns its value, but a missing key with no default raises KeyError. Passing a second argument makes it return that instead.
-
3. 🧋 What is 'key' in {'key':1}?
Show answer
Answer
A. TrueWhy: The in operator checks keys, and 'key' is the key of this dictionary, so the expression is True. The value 1 is irrelevant here.
-
4. In what order do ordinary Python 3.7+ dictionaries store keys?
Show answer
Answer
A. insertion orderWhy: Since Python 3.7 the language guarantees insertion order for dict, so iteration follows the order keys were added. Alphabetical order requires sorted().
-
5. Test whether “bubble tea” is in menu dictionary d: "bubble tea" ___ d
Show answer
Answer
inWhy: in tests keys, so 'bubble tea' in d is True only if that string is a key; it does not look at the values. Membership never inspects values.
-
6. 🧋 Safely read a dictionary value: d.___('key', 'default')
Show answer
Answer
getWhy: get takes the key first and the fallback second, so d.get('key', 'default') never raises. Without the fallback it returns None.
-
7. Make an independent copy of the menu. Which method goes in the blank?
backup = menu.____()Show answer
Answer
copyWhy: copy() returns a shallow copy, so adding keys to backup will not touch menu; nested values are still shared. Use deepcopy for nested data.
-
8. How do get and setdefault differ, and what is printed?
d = {"a": 1} print(d.get("b", 0), d.setdefault("c", 2), d)Show answer
Answer
0 2 {'a': 1, 'c': 2}Why: get('b', 0) returns 0 without adding b, while setdefault('c', 2) returns 2 and does insert c. Hence 0, 2, and {'a': 1, 'c': 2}.
-
9. setdefault inserts the missing b key. What is returned?
d = {"a": 1} print(d.setdefault("b", 2), d)Show answer
Answer
2 {'a': 1, 'b': 2}Why: setdefault returns the existing value if the key is there, otherwise inserts the default and returns it. Here b was missing, so 2 is returned and b appears in d.
-
10. What does `score('quirky')` return?
POINTS = { 'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4, 'g': 2, 'h': 4, 'i': 1, 'j': 8, 'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1, 'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1, 'u': 1, 'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10 } def score(word): if not word.isalpha(): return 0 return sum(POINTS[letter] for letter in word.lower()) print(score('quirky'))Show answer
Answer
22Why: q 10, u 1, i 1, r 1, k 5 and y 4 add up to 22, so score('quirky') returns 22. Nothing is deduplicated, so a repeated letter is counted again.
Common questions
What is the difference between get and square brackets?
d[key] raises KeyError when the key is missing. d.get(key) returns None, and d.get(key, default) returns your fallback.
Do dictionaries keep insertion order?
Current Python versions preserve insertion order as part of the language behaviour, but you should still write code that does not depend on order unless order is part of the requirement.
Keep the streak going.
The full bank keeps your place in this browser, filters by topic, and includes a daily question and mistake review.