3/29
Part 1 of 6 · The formatLearn and do

A missing record or field answers None

Next, get:

  • get(key, field) returns the value, or None if the record or the field doesn't exist.

There are two ways to be missing: no record at all, or a record without that field. dict.get handles both without an error: it returns None for a missing key, or a default that you pass it. Here it is on a phone book, a dict of dicts like yours:

phones = {"ana": {"home": "555-0101"}}
print(phones.get("ana", {}).get("home"))
print(phones.get("ana", {}).get("work"))
print(phones.get("ben", {}).get("home"))
555-0101
None
None

For a missing person, phones.get("ben", {}) gives an empty dict, so the second .get still has a dict to work on. Square brackets, phones["ben"], would raise a KeyError instead.

In the editor, set is written, and you write get. It prints Ada, then None twice.