If a list is just a dictionary whose indices must be 0, 1, 2 in order, what becomes possible once you choose the keys yourself?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
An astronomer hands you this table of planets and their confirmed moon counts:
| planet | moons |
|---|---|
| Mars | 2 |
| Jupiter | 95 |
| Neptune | 14 |
Write the whole table as a single Python dictionary — the planet names (strings) are the keys, the moon counts (integers) are the values — and assign it to moons. Keep the rows in the order shown.
A list as a restrictive dictionary
Define an entry as a key mapped to a dictionary value, and set a list's fixed indices 0, 1, 2 in order beside a dictionary's arbitrary custom keys.
Building a dictionary with curly braces
Write my_dict = {}, d = {4: 16} and grades = {'Ana': 'B', 'Matt': 'A', 'John': 'B', 'Katy': 'A'}, marking the key, the colon and the dictionary value in each entry.
Lookup by key, and KeyError
Record that grades['John'] evaluates to 'B' the way L[3] evaluates to an element, that grades['Grace'] raises KeyError, and that no reverse value-to-key lookup exists.
The find_grades exercise
Write the loop that builds Lnew by looking up grades[elem] for each name, and run it on d with ['Matt', 'Katy'] to get ['C', 'A'].