Dictionary Key Lookup¶
Run the Dictionary Key Lookup MicroSim Fullscreen
Edit in the p5.js Editor
About This MicroSim¶
Why is dictionary lookup instant, and why does a missing key crash? This sim
draws the dictionary as a cabinet of drawers with keys written on the fronts.
A typed key springs the matching drawer open in "one hop"; a missing key
shows the real KeyError banner — or, with the .get() toggle, a polite
None. The comparison mode puts the same data in a list and counts how many
items must be checked one by one, planting the efficiency seed without any
hash-table jargon.
Learning objective: The student will be able to explain how a dictionary
finds a value from a key and why a missing key raises KeyError while .get()
does not.
- Bloom's Taxonomy (2001): Understand — explain, compare, infer
- Interaction pattern: type-a-key lookup with a list-vs-dict comparison mode
How to Use¶
- Look up
grace— the drawer opens in one hop. - Try
Gracewith a capital G: keys must match exactly, so you get aKeyError. - Check use .get() and repeat:
Noneinstead of a crash. - Check compare with a list search and look up
guido(the last drawer): the list checks all five items; the dictionary still takes one hop.
Iframe Embed Code¶
You can add this MicroSim to any web page by adding this to your HTML:
1 2 3 4 | |
Lesson Plan¶
Grade Level¶
Upper elementary and middle school (ages 10-14), Chapter 21 (Dictionaries)
Duration¶
10 minutes
Prerequisites¶
- Creating dictionaries and accessing values by key (Chapter 21 opening sections)
- Lists and the in operator (Chapters 15-16)
Activities¶
- Exact-match drill (3 min): Students predict the outcome for
grace,Grace, andgracie, then test all three. - Crash vs None (3 min): Same missing key with brackets and with .get(); students write one sentence on when they would choose each.
- The race (4 min): With comparison on, students look up the first, middle, and last keys and record the list's check counts vs the dict's.
Assessment¶
- Student can explain why
phone_book["Grace"]fails when the key is"grace" - Student can state what
.get()returns for a missing key - Student can explain why dictionaries are faster than lists for lookups
References¶
- Python Tutorial — Dictionaries — official documentation
- p5.js — the JavaScript library used to build this MicroSim