Tool returns 10 messages. You only want the ones whose subject contains the letter a. The filter is in your code, not in the API call.
A for loop with an if inside?
That's the explicit way:
matching = []
for m in messages:
if "a" in m.get("subject", ""):
matching.append(m)
print(len(matching))Works in any track. Cleaner Python — though we won't drill it until Intermediate — is a list comprehension:
matching = [m for m in messages if "a" in m.get("subject", "")]
print(len(matching))Same logic, one line. Either is fine for now.
When should I use the API's filter vs Python's filter?
Push to the API when you can. Gmail's query parameter (which we'll meet in time-windows) lets the server filter — only matching messages cross the network. Faster, cheaper, no quota wasted. But for client-side conditions the API can't express, Python filtering is fine. Today's exercise is purely Python.
The tool returns a list. You want a subset. Python's for + if:
result = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": 10})
messages = result.get("messages", [])
matching = []
for m in messages:
subject = m.get("subject", "")
if "a" in subject:
matching.append(m)
print(len(matching))Nothing new — for, if, .get, .append, len. The list filtering pattern from Python Beginner, applied to dicts that came from an API call.
Python has a one-line shorthand for the loop+if+append pattern:
matching = [m for m in messages if "a" in m.get("subject", "")]Reads: "a list of m for each m in messages where "a" is in m.get("subject", "")". Same result, less code. Python Intermediate goes deep on these — for now, treat it as a shortcut you can use when you're comfortable.
For Gmail specifically, the API accepts a query parameter using Gmail's search syntax — from:boss@co.com, is:unread, subject:invoice. That filtering happens on Google's side, only matching messages come back:
result = toolset.execute_action(
Action.GMAIL_FETCH_EMAILS,
{"query": "is:unread", "max_results": 50}
)Use server-side filtering when you can — it's faster and uses less quota than fetching everything and filtering locally. Day 17 of this track explores time-window queries.
Tool returns 10 messages. You only want the ones whose subject contains the letter a. The filter is in your code, not in the API call.
A for loop with an if inside?
That's the explicit way:
matching = []
for m in messages:
if "a" in m.get("subject", ""):
matching.append(m)
print(len(matching))Works in any track. Cleaner Python — though we won't drill it until Intermediate — is a list comprehension:
matching = [m for m in messages if "a" in m.get("subject", "")]
print(len(matching))Same logic, one line. Either is fine for now.
When should I use the API's filter vs Python's filter?
Push to the API when you can. Gmail's query parameter (which we'll meet in time-windows) lets the server filter — only matching messages cross the network. Faster, cheaper, no quota wasted. But for client-side conditions the API can't express, Python filtering is fine. Today's exercise is purely Python.
The tool returns a list. You want a subset. Python's for + if:
result = toolset.execute_action(Action.GMAIL_FETCH_EMAILS, {"max_results": 10})
messages = result.get("messages", [])
matching = []
for m in messages:
subject = m.get("subject", "")
if "a" in subject:
matching.append(m)
print(len(matching))Nothing new — for, if, .get, .append, len. The list filtering pattern from Python Beginner, applied to dicts that came from an API call.
Python has a one-line shorthand for the loop+if+append pattern:
matching = [m for m in messages if "a" in m.get("subject", "")]Reads: "a list of m for each m in messages where "a" is in m.get("subject", "")". Same result, less code. Python Intermediate goes deep on these — for now, treat it as a shortcut you can use when you're comfortable.
For Gmail specifically, the API accepts a query parameter using Gmail's search syntax — from:boss@co.com, is:unread, subject:invoice. That filtering happens on Google's side, only matching messages come back:
result = toolset.execute_action(
Action.GMAIL_FETCH_EMAILS,
{"query": "is:unread", "max_results": 50}
)Use server-side filtering when you can — it's faster and uses less quota than fetching everything and filtering locally. Day 17 of this track explores time-window queries.
Create a free account to get started. Paid plans unlock all tracks.