A flat list hides where each item came from. A dict keyed by source preserves origin. What should the return shape be?
Something like {"gmail": [...], "calendar": [...], "tasks": [...]} where each key maps to that source's list?
Exactly. Three comprehensions, three named keys, one dict. No concatenation — each source lives in its own list so the caller can process them independently or together:
result = {
"gmail": [m.get("snippet", "") for m in emails.get("messages", [])],
"calendar": [e.get("summary", "") for e in events.get("items", [])],
"tasks": [t.get("title", "") for t in tasks.get("items", [])],
}So the return is the opposite of yesterday's concatenation — instead of one list, three labeled lists.
Exactly. Downstream code can say result["gmail"] to get just email content or iterate over .values() for all of it. Here's the full function with the three reads wrapped in:
def group_by_source(max_items: int) -> dict:
emails = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": max_items})
events = toolset.execute_action(Action.GOOGLECALENDAR_FIND_EVENT, {"query": ""})
tasks = toolset.execute_action(Action.GOOGLETASKS_LIST_TASKS, {"max_results": max_items})
result = {
"gmail": [m.get("snippet", "") for m in emails.get("messages", [])],
"calendar": [e.get("summary", "") for e in events.get("items", [])],
"tasks": [t.get("title", "") for t in tasks.get("items", [])],
}
print(f"Grouped: gmail={len(result['gmail'])} calendar={len(result['calendar'])} tasks={len(result['tasks'])}")
return resultWhat if I want to add a fourth source later — Slack, say?
Add a fourth read, a fourth comprehension, a fourth key. Each source is isolated, so additions are local — no existing code changes. That is what makes the dict-of-lists shape the right one for heterogeneous sources.
And the printed counts tell me how much each source contributed, live, right at that moment?
Three named lists, one dict, three live counts in the log. The moment you need to act differently per source, this is the shape your code wants.
TL;DR: Return {"gmail": [...], "calendar": [...], "tasks": [...]} to keep origin without losing composability.
.get()| Shape | Use when |
|---|---|
| flat list | one uniform downstream pass |
| dict of lists | per-source logic or diagnostics |
A flat list hides where each item came from. A dict keyed by source preserves origin. What should the return shape be?
Something like {"gmail": [...], "calendar": [...], "tasks": [...]} where each key maps to that source's list?
Exactly. Three comprehensions, three named keys, one dict. No concatenation — each source lives in its own list so the caller can process them independently or together:
result = {
"gmail": [m.get("snippet", "") for m in emails.get("messages", [])],
"calendar": [e.get("summary", "") for e in events.get("items", [])],
"tasks": [t.get("title", "") for t in tasks.get("items", [])],
}So the return is the opposite of yesterday's concatenation — instead of one list, three labeled lists.
Exactly. Downstream code can say result["gmail"] to get just email content or iterate over .values() for all of it. Here's the full function with the three reads wrapped in:
def group_by_source(max_items: int) -> dict:
emails = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": max_items})
events = toolset.execute_action(Action.GOOGLECALENDAR_FIND_EVENT, {"query": ""})
tasks = toolset.execute_action(Action.GOOGLETASKS_LIST_TASKS, {"max_results": max_items})
result = {
"gmail": [m.get("snippet", "") for m in emails.get("messages", [])],
"calendar": [e.get("summary", "") for e in events.get("items", [])],
"tasks": [t.get("title", "") for t in tasks.get("items", [])],
}
print(f"Grouped: gmail={len(result['gmail'])} calendar={len(result['calendar'])} tasks={len(result['tasks'])}")
return resultWhat if I want to add a fourth source later — Slack, say?
Add a fourth read, a fourth comprehension, a fourth key. Each source is isolated, so additions are local — no existing code changes. That is what makes the dict-of-lists shape the right one for heterogeneous sources.
And the printed counts tell me how much each source contributed, live, right at that moment?
Three named lists, one dict, three live counts in the log. The moment you need to act differently per source, this is the shape your code wants.
TL;DR: Return {"gmail": [...], "calendar": [...], "tasks": [...]} to keep origin without losing composability.
.get()| Shape | Use when |
|---|---|
| flat list | one uniform downstream pass |
| dict of lists | per-source logic or diagnostics |
Create a free account to get started. Paid plans unlock all tracks.