Dictionaries in Python
Chapter 11: Dictionaries
5 short lessons, with 3D animations, quizzes and coding tasks.
The lessons
Making dictionaries
A dictionary stores key: value pairs, and d[key] opens the locker with that key and gives you its value.
Changing a dictionary
d[key] = value adds or changes a pair, del d[key] removes one, and get and pop read and remove without crashing on a missing key.
Looping over a dictionary
for key in d: visits every key in the order they were added, and d.items() hands you each key with its value.
Counting with a dictionary
To count things, start with an empty dictionary and, for each item, do counts[x] = counts.get(x, 0) + 1.
Dictionaries in practice
A dictionary's values can be lists or dictionaries, which lets you group items under a key and look things up in one step.