Programming Essentials · BE5B33PRG · Topic 4 of 9
Dictionaries, sets and hashability
Choose keys, membership and uniqueness deliberately.
Key ideas
Dictionary
counts = {"a": 2}; counts["a"] is 2. Use counts.get(ch, 0) + 1 when building a frequency table.
Set
seen = set(); seen.add("x") stores one unique member. Membership is tested with "x" in seen.
Keys
Dictionary keys and set elements must be hashable; a tuple of immutable values can be a key, a list cannot.
Iteration
for key, value in counts.items(): traverses pairs. Do not rely on set iteration order.
In one line: Use a dict for key–value lookup and a set for unique membership.
Sources
Written by Ucenly. Based on the BE5B33PRG course materials:
- lec05-collections-mn-2025_24_10_v2, p. 10, 20, 23, 38