Chapter 4
Dicts and sets
A dict pairs keys with values, like a word and its meaning, and finds any value by its key in an instant. A set holds each value once, with no order. This chapter covers looking up and updating keys, get() with a default, what happens with a missing key, looping over a dict, removing repeats with a set, finding what two sets share, and which values can be keys.
11 cards
See the 11 cards
- What does looking up Bo print?25
- What does len() print for a dict?2
- What's wrong here: looking up Zoe?KeyError
- What does .get() print for a missing key?0
- How many items does this set hold?3
- What does the dict hold after the update?{'a': 5}
- What does looping over a dict print?xy
- What does & print for two sets?{2, 3}
- Fill the blank to test if pear is in the setin
- Brackets used to write a dictCurly brackets
- Can a list be a dict key?No
Image credits
- What does looking up Bo print?: Brain Zap, CC0 1.0 Source
- What does len() print for a dict?: Brain Zap, CC0 1.0 Source
- What's wrong here: looking up Zoe?: Brain Zap, CC0 1.0 Source
- What does .get() print for a missing key?: Brain Zap, CC0 1.0 Source
- How many items does this set hold?: Brain Zap, CC0 1.0 Source
- What does the dict hold after the update?: Brain Zap, CC0 1.0 Source
- What does looping over a dict print?: Brain Zap, CC0 1.0 Source
- What does & print for two sets?: Brain Zap, CC0 1.0 Source
- Fill the blank to test if pear is in the set: Brain Zap, CC0 1.0 Source