You have a dict of category totals — {"Food": 120.0, "Coffee": 45.0}. You want to read totals["Rent"]. What happens?
Python crashes? There's no "Rent" key in that dict.
It raises a KeyError. Direct bracket access assumes the key is there. When the key might be missing, you reach for .get() instead — it takes a key and a default:
totals.get("Rent", 0.0)If "Rent" exists, you get its value. If it doesn't, you get 0.0 back. No crash.
So .get() silently returns the default even when the key doesn't exist? That feels dangerous.
It's a deliberate trade. When you expect a key to be there, use totals[key] — a crash is a useful signal. When you know some keys will be missing and that's fine, use .get() with a sensible default.
What's a sensible default? Is it always zero?
It depends on what you're totaling. For money totals, 0.0 means "no spend here yet" — safe to add to. For a count, 0. For a list, []. You match the default to the type the caller expects.
def get_category_total(totals, category):
return totals.get(category, 0.0)So the function is one line — .get() handles the missing case for me and returns whatever default makes sense.
One line, no branching, no crashes. This is the pattern you'll reach for every time you read from a dict where a missing key is part of normal life, not a broken contract.
dict.get(key, default)TL;DR: safe lookup — returns the default when the key is missing.
d[key] — direct access, raises KeyError if missingd.get(key) — returns None if missingd.get(key, default) — returns default if missing| Type | Default |
|---|---|
float total | 0.0 |
int count | 0 |
list | [] |
str | "" |
Use d[key] when a missing key is a bug. Use d.get(key, default) when a missing key is normal.
Write `get_category_total(totals, category)` that returns `totals[category]` if the category exists, or `0.0` if not. Example: `get_category_total({"Food": 120.0}, "Rent")` returns `0.0`.
Tap each step for scaffolded hints.
No blank-editor panic.
You have a dict of category totals — {"Food": 120.0, "Coffee": 45.0}. You want to read totals["Rent"]. What happens?
Python crashes? There's no "Rent" key in that dict.
It raises a KeyError. Direct bracket access assumes the key is there. When the key might be missing, you reach for .get() instead — it takes a key and a default:
totals.get("Rent", 0.0)If "Rent" exists, you get its value. If it doesn't, you get 0.0 back. No crash.
So .get() silently returns the default even when the key doesn't exist? That feels dangerous.
It's a deliberate trade. When you expect a key to be there, use totals[key] — a crash is a useful signal. When you know some keys will be missing and that's fine, use .get() with a sensible default.
What's a sensible default? Is it always zero?
It depends on what you're totaling. For money totals, 0.0 means "no spend here yet" — safe to add to. For a count, 0. For a list, []. You match the default to the type the caller expects.
def get_category_total(totals, category):
return totals.get(category, 0.0)So the function is one line — .get() handles the missing case for me and returns whatever default makes sense.
One line, no branching, no crashes. This is the pattern you'll reach for every time you read from a dict where a missing key is part of normal life, not a broken contract.
dict.get(key, default)TL;DR: safe lookup — returns the default when the key is missing.
d[key] — direct access, raises KeyError if missingd.get(key) — returns None if missingd.get(key, default) — returns default if missing| Type | Default |
|---|---|
float total | 0.0 |
int count | 0 |
list | [] |
str | "" |
Use d[key] when a missing key is a bug. Use d.get(key, default) when a missing key is normal.