Dictionary methods

18-minute lesson · module-dictionaries-02

You are previewing this course as a guest

Read anything and run any exercise you like. It all runs in your own browser, so nothing here is saved: no XP, no progress, and the AI tutor needs an account. Sign in and your work starts counting.

Sign in

Dictionary methods

18-minute lesson · module-dictionaries-02

  • Use `.get` with a default to read a key that might be missing.
  • Iterate with `.keys`, `.values`, and `.items`.
  • Apply `.setdefault` and `.update` to build dictionaries incrementally.
  • Implement the count-with-a-dictionary pattern (tally how often each item appears).

Source attribution

Status: adapted

How to Think Like a Computer Scientist: Interactive Edition source · Dictionaries / Dictionary methods

Adaptation notes. Reading rewritten in our voice. Worked example tallies student enrollment by department using an Iowa State CS course-registration theme. Hidden tests check the counting pattern and the difference between `.get` and direct subscripting.

License notes. Original chapter is GNU FDL 1.3 (Brad Miller, David Ranum, and the upstream Elkner/Downey/Meyers text). No verbatim text reproduced.