Python dictionary exercises
Dictionaries are how real Python programs hold data, and they fail in a few very specific ways: a key that is not there, a loop that only sees keys, a dict changed while you walk through it. Each exercise here is one of those, in a short function with tests.
Find the line, fix it, run the tests. Everything runs in your browser.
The dictionaries bugs you will meet
- d[key] raises KeyError for a missing key; d.get(key, default) does not.
- for x in d loops over keys only; use d.items() for keys and values together.
- Adding or deleting keys while looping over the same dict raises RuntimeError.
- .get(key) or default replaces a stored 0 or empty string too.
- Passing a dict into a function and changing it changes the caller's dict.
The exercises
Medium
Errors you might be searching for
More Python practice by topic
Questions
How hard are these?
Mostly medium: dictionaries tend to come after lists and strings in a course. Try the Python list exercises first if these feel steep.
Do they run real Python?
Yes, Python 3 compiled to WebAssembly (Pyodide), in your browser. Nothing is sent to a server.