Skip to content
TaeyoungKim.dev

Python dict.get() vs. Square Brackets: KeyError, None, and Defaults

PythonWritten 2 min readTaeyoungKim
LinkedInX

Reading profile['city'] stops the program when a profile has no city. Replacing the lookup with get() avoids the error but returns None. That may seem convenient, yet it can silently hide a missing required field. First decide whether absence should fail or whether processing should continue with another value.

Why do square brackets raise KeyError?

For a dictionary, the value inside square brackets is a key, not a list position. This profile contains only name:

python
profile = {'name': 'Ada'}

print(profile['name'])
print(profile['city'])

The first lookup prints Ada. The second asks for a missing city key and raises KeyError: 'city'. A misspelled key fails the same way. If the key must exist, that failure can reveal the problem early.

What does dict.get() return for a missing key?

With the same profile, get() returns None instead of raising an exception. Its second argument supplies a default. The Python dictionary documentation describes this behavior.

python
profile = {'name': 'Ada'}

print(profile.get('city'))
print(profile.get('city', 'Not provided'))
print(profile)
text
None
Not provided
{'name': 'Ada'}

get() reads the dictionary; it does not add a city entry. The diagram compares two ways of looking up the same missing key:

profile['city'] raises KeyError; profile.get('city') returns None. Neither lookup inserts the missing key.

get() is not automatically the safer choice. A typo such as profile.get('nmae') also returns None. Using it for every required field can make typos and legitimate missing values hard to tell apart.

How do you distinguish a stored None from an absent key?

Now nickname exists, but its value is None:

python
profile = {'name': 'Ada', 'nickname': None}

print(profile.get('nickname'))  # Present, with value None
print(profile.get('city'))      # Absent
print('nickname' in profile)
print('city' in profile)
text
None
None
True
False

The return value from get() is the same in both cases. If presence matters, check key in dictionary. Even get('nickname', 'Not provided') returns the stored None; the fallback applies only when the key is absent.

In real input handling, separate required fields from optional ones. For a required name, check presence or handle a failed bracket lookup. For an optional city, get('city', 'Not provided') may be a suitable display value. Whether an explicitly stored None should also display as “Not provided” is a separate data rule.

Key takeaways

profile['city'] raises KeyError when the key is absent; profile.get('city') returns None. The default in get(key, default) applies only to an absent key. If you must distinguish absence from a stored None, use key in profile, and avoid silently hiding a missing required field.

Author

TaeyoungKim

Connecting technical foundations with implementation, verification, and production decisions.

#Python#dictionary#dict.get#KeyError#None

Read next