Yesterday the response was a dict — <class 'dict'>. The interesting thing about that dict isn't the dict itself, it's what's inside. For GMAIL_FETCH_EMAILS, the dict has a key "messages" holding a list of email objects.
result = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": 3})
messages = result.get("messages", [])
print(len(messages))Why .get("messages", []) and not result["messages"]?
Defensive parsing. If the inbox is empty, the API may drop the messages key entirely. result["messages"] would KeyError. result.get("messages", []) falls back to an empty list, so len() returns 0 cleanly. Make .get(key, default) your reflex when reading API response dicts — you don't always know which keys are present.
And len(messages) — same len from week 1?
Same len. A list of API response objects is just a list. Everything you learned in Python applies — len, indexing, for, in, append. The API call hands you back a normal Python value; from there you're back on familiar ground.
Many tools return a dict with a list inside. The list contains one element per item the API matched.
result = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": 5})
messages = result.get("messages", []) # list of dicts
print(len(messages)) # how many came back.get(key, default) reflexAPI response shapes vary by state. An empty inbox might return {} instead of {"messages": []}. Defensive reading:
messages = result.get("messages", []) # always a listNever crashes on a missing key. If you write result["messages"] and the key happens to be missing, your script ends with KeyError. The default-empty-list pattern is the canonical fix.
Once you have messages, it's a list. Everything you learned:
len(messages) # count
messages[0] # first item
for m in messages: ... # iterate
len(messages) > 0 # "any results?"| Tool | Returned dict shape (key holding the list) |
|---|---|
GMAIL_FETCH_EMAILS | messages |
GOOGLESHEETS_BATCH_GET | valueRanges |
SLACK_LIST_MEMBERS | members |
NOTION_QUERY_DATABASE | results |
The key name is in each tool's docs; for v1 lessons we'll always tell you the key in the problem statement.
Yesterday the response was a dict — <class 'dict'>. The interesting thing about that dict isn't the dict itself, it's what's inside. For GMAIL_FETCH_EMAILS, the dict has a key "messages" holding a list of email objects.
result = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": 3})
messages = result.get("messages", [])
print(len(messages))Why .get("messages", []) and not result["messages"]?
Defensive parsing. If the inbox is empty, the API may drop the messages key entirely. result["messages"] would KeyError. result.get("messages", []) falls back to an empty list, so len() returns 0 cleanly. Make .get(key, default) your reflex when reading API response dicts — you don't always know which keys are present.
And len(messages) — same len from week 1?
Same len. A list of API response objects is just a list. Everything you learned in Python applies — len, indexing, for, in, append. The API call hands you back a normal Python value; from there you're back on familiar ground.
Many tools return a dict with a list inside. The list contains one element per item the API matched.
result = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": 5})
messages = result.get("messages", []) # list of dicts
print(len(messages)) # how many came back.get(key, default) reflexAPI response shapes vary by state. An empty inbox might return {} instead of {"messages": []}. Defensive reading:
messages = result.get("messages", []) # always a listNever crashes on a missing key. If you write result["messages"] and the key happens to be missing, your script ends with KeyError. The default-empty-list pattern is the canonical fix.
Once you have messages, it's a list. Everything you learned:
len(messages) # count
messages[0] # first item
for m in messages: ... # iterate
len(messages) > 0 # "any results?"| Tool | Returned dict shape (key holding the list) |
|---|---|
GMAIL_FETCH_EMAILS | messages |
GOOGLESHEETS_BATCH_GET | valueRanges |
SLACK_LIST_MEMBERS | members |
NOTION_QUERY_DATABASE | results |
The key name is in each tool's docs; for v1 lessons we'll always tell you the key in the problem statement.
Create a free account to get started. Paid plans unlock all tracks.