Module 6: Dictionaries and sets

Look things up by key: dictionaries, records, counting and sets, and how to pick the right collection.

  1. Dictionaries: d[k] crashes on a missing key, d.get(k, default) does not; in checks keys; count with d[x] = d.get(x, 0) + 1.
  2. Records: lists of dicts and dicts of lists: A record is a dict, a table is a list of dicts; group with d.setdefault(k, []).append(x); use fromkeys only with immutable starting values.
  3. Sets, and choosing the right collection: A set keeps unique values (set() is empty, {} is a dict); & | - combine sets; keys must be hashable, so use a tuple, never a list.

Ends with a 13-question checkpoint (80% passes) and a mini project: Canteen Counter: menu, orders and stock.