Capstone for this week. One function reads both sources and hands back a dict with four keys — two counts and two first items. How would you reach into a list and pull out its first element without crashing on an empty list?
messages[0] would crash if the list is empty. Is there a safe version that gives me None or an empty string instead?
Use indexing guarded by a length check, or the next() trick on an iterator. The simplest version is a one-line conditional: value if the list is non-empty, a fallback otherwise:
first_email = messages[0]["snippet"] if messages else ""
first_event = items[0]["summary"] if items else ""So the whole function combines every pattern we used this week — two reads, count each, extract first item, shape into a dict?
That's the full recipe. Four keys, one return, no branches outside the first-item guard:
def morning_overview(max_emails: int, max_events: int) -> dict:
emails = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": max_emails})
events = toolset.execute_action(Action.GOOGLECALENDAR_FIND_EVENT, {"query": ""})
msgs = emails.get("messages", [])
itms = events.get("items", [])
return {
"email_count": len(msgs),
"event_count": len(itms),
"first_email": msgs[0].get("snippet", "") if msgs else "",
"first_event": itms[0].get("summary", "") if itms else "",
}What if Gmail returns messages but the first one has no snippet? The conditional protects against empty lists — does .get() on the dict protect the rest?
Two layers of defence — the outer conditional handles empty lists; the inner .get() handles missing fields. Together they cover every shape the API can hand back. Your dict always has four keys, always with the right types.
So after five lessons I can write a full morning overview of my day in thirteen lines — four API keys extracted, two sources merged?
Thirteen lines, two APIs, one dict. That is the entire first-week skeleton. The next three weeks stack analysis, writes, and defence on top of exactly this shape.
TL;DR: Two reads, two first-item guards, one flat dict with four keys.
len(list) on each responselist[0].get(field, "") if list else ""| Failure | Handled by |
|---|---|
| Missing list key | .get(key, []) |
| Empty list | if list else "" |
| Missing field | .get(field, "") |
Every layer is a one-liner — stack all three and your function never raises on any API shape.
Capstone for this week. One function reads both sources and hands back a dict with four keys — two counts and two first items. How would you reach into a list and pull out its first element without crashing on an empty list?
messages[0] would crash if the list is empty. Is there a safe version that gives me None or an empty string instead?
Use indexing guarded by a length check, or the next() trick on an iterator. The simplest version is a one-line conditional: value if the list is non-empty, a fallback otherwise:
first_email = messages[0]["snippet"] if messages else ""
first_event = items[0]["summary"] if items else ""So the whole function combines every pattern we used this week — two reads, count each, extract first item, shape into a dict?
That's the full recipe. Four keys, one return, no branches outside the first-item guard:
def morning_overview(max_emails: int, max_events: int) -> dict:
emails = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": max_emails})
events = toolset.execute_action(Action.GOOGLECALENDAR_FIND_EVENT, {"query": ""})
msgs = emails.get("messages", [])
itms = events.get("items", [])
return {
"email_count": len(msgs),
"event_count": len(itms),
"first_email": msgs[0].get("snippet", "") if msgs else "",
"first_event": itms[0].get("summary", "") if itms else "",
}What if Gmail returns messages but the first one has no snippet? The conditional protects against empty lists — does .get() on the dict protect the rest?
Two layers of defence — the outer conditional handles empty lists; the inner .get() handles missing fields. Together they cover every shape the API can hand back. Your dict always has four keys, always with the right types.
So after five lessons I can write a full morning overview of my day in thirteen lines — four API keys extracted, two sources merged?
Thirteen lines, two APIs, one dict. That is the entire first-week skeleton. The next three weeks stack analysis, writes, and defence on top of exactly this shape.
TL;DR: Two reads, two first-item guards, one flat dict with four keys.
len(list) on each responselist[0].get(field, "") if list else ""| Failure | Handled by |
|---|---|
| Missing list key | .get(key, []) |
| Empty list | if list else "" |
| Missing field | .get(field, "") |
Every layer is a one-liner — stack all three and your function never raises on any API shape.
Create a free account to get started. Paid plans unlock all tracks.