Lesson 17 of 26 · about 16 min
Dictionaries and sets
A dictionary looks things up by a label (key) instead of a position. A set keeps only unique items and answers "is it in here?" instantly.
🧭 What you need first: Tuples and list comprehensions
A list is like a row of numbered lockers: to find something, you need its position. But you don’t think about your friends by position (“friend number 4”); you think by name.
A dictionary works like a real dictionary or a phone’s contacts: you look things up by a label. “What’s Ada’s number?” Python jumps straight to the answer without searching through everything. The label is called a key, and what it points to is the value.
And sometimes you only care whether something is there, not where. A guest list at a party: is Sam on it? Duplicates don’t matter, and order doesn’t matter. That’s a set.
Another way: a coat check (again!)
Ticket number → coat. You hand over the ticket (key), you get the coat (value). Two coats can’t share a ticket, but two tickets could hold identical coats.
Another way: a menu
Dish name → price. “How much is the soup?” You look up “soup” and read the price. You’d never say “how much is dish number 7?”
Count the words
This is the counting pattern, one of the most useful bits of code you’ll ever learn. Step through and watch the dictionary fill up.
Watch the code run, one step at a time
- 1words = "the cat and the hat and the bat".split()
- 2counts = {}
- 3for w in words:
- 4 counts[w] = counts.get(w, 0) + 1
- 5print(counts)
- 6print("Unique words:", set(words))
➜ yellow = the line about to run · ✓ = the line that just ran
The real words
Dictionaries map keys to values: {key: value, key: value}.
| Operation | What it does |
|---|---|
d[key] |
get the value (a KeyError if the key is missing) |
d.get(key, default) |
get the value, or default if missing |
d[key] = value |
add a new pair, or replace the value |
key in d |
is this key present? |
del d[key] / d.pop(key) |
remove a pair |
d.keys(), d.values(), d.items() |
everything, for looping |
- Keys must be unique and immutable (strings, numbers, tuples; not lists). Values can be anything, even lists or other dictionaries.
- Dictionaries remember insertion order when you loop over them.
- Lookup is fast no matter how big the dictionary gets. How that magic works (hashing) is a whole topic in F10.
Sets are unordered collections of unique, immutable items: {"red", "green"}. (An empty set is set(), because {} is an empty dictionary.)
set([1, 1, 2])→{1, 2}: the easiest way to remove duplicates.x in sis fast, like dictionary lookup.- Set math:
a | b(in either),a & b(in both),a - b(in a but not b).
Word counts
Write word_counts(text) that returns a dictionary of how many times each word appears. Ignore case: 'The' and 'the' are the same word.
Who came to both parties?
Using set operations, set both to the people at BOTH parties, only_first to people at the first but not the second, and everyone to the number of different people across both.
prices = {"tea": 2}. What does prices.get("coffee", 0) return?
Got it?
Finish the lesson to color it in on your map and collect its concept cards.
Did this lesson click?