You know how to count results. Now you want the one-sentence description of the top hit — what the engine ranks first for your query. How do you reach it inside the list?
The list is ordered by relevance, so results[0] is the top result. And each item has title, url, snippet — so the snippet is results[0]['snippet'].
Exactly. Dict access with square brackets, not dot notation, because each result is a plain dict. The smallest version of the function:
results = search("python decorators", count=3)
top_snippet = results[0]["snippet"]
print(top_snippet)So results[0] gets the first dict, then ['snippet'] pulls one field out of it. Two steps, no intermediate variable needed.
You can chain them. Inside a function:
def search_first_snippet(query: str) -> str:
results = search(query, count=5)
return results[0]["snippet"]Notice count=5 rather than count=1 — asking for more gives the engine a larger pool to rank against, which tends to surface a stronger top hit.
What happens if the engine returns nothing for a ridiculous query? Does results[0] raise an error?
Yes — IndexError on an empty list. For this lesson every query returns results, so the straight access is safe. In production you would guard with if not results: return "" — defensive coding, but the core move is still the indexed access.
So one call, two lookups, and I have the engine's best one-line summary of the live web for my query.
Exactly. The snippet is the most information-dense field in a search result — engines optimize it for human scanning. That makes it the ideal input for the agents you'll wire in during Week 2.
TL;DR: results[0]['snippet'] is the top hit's description string.
results[0] — first dict (most relevant)['snippet'] — dict key access with square bracketscount=5 — asking for a few gives better ranking| Key | Type | Content |
|---|---|---|
title | str | Page title |
url | str | Absolute URL |
snippet | str | One-line description |
An IndexError on an empty list is your friend — it tells you the query found nothing.
You know how to count results. Now you want the one-sentence description of the top hit — what the engine ranks first for your query. How do you reach it inside the list?
The list is ordered by relevance, so results[0] is the top result. And each item has title, url, snippet — so the snippet is results[0]['snippet'].
Exactly. Dict access with square brackets, not dot notation, because each result is a plain dict. The smallest version of the function:
results = search("python decorators", count=3)
top_snippet = results[0]["snippet"]
print(top_snippet)So results[0] gets the first dict, then ['snippet'] pulls one field out of it. Two steps, no intermediate variable needed.
You can chain them. Inside a function:
def search_first_snippet(query: str) -> str:
results = search(query, count=5)
return results[0]["snippet"]Notice count=5 rather than count=1 — asking for more gives the engine a larger pool to rank against, which tends to surface a stronger top hit.
What happens if the engine returns nothing for a ridiculous query? Does results[0] raise an error?
Yes — IndexError on an empty list. For this lesson every query returns results, so the straight access is safe. In production you would guard with if not results: return "" — defensive coding, but the core move is still the indexed access.
So one call, two lookups, and I have the engine's best one-line summary of the live web for my query.
Exactly. The snippet is the most information-dense field in a search result — engines optimize it for human scanning. That makes it the ideal input for the agents you'll wire in during Week 2.
TL;DR: results[0]['snippet'] is the top hit's description string.
results[0] — first dict (most relevant)['snippet'] — dict key access with square bracketscount=5 — asking for a few gives better ranking| Key | Type | Content |
|---|---|---|
title | str | Page title |
url | str | Absolute URL |
snippet | str | One-line description |
An IndexError on an empty list is your friend — it tells you the query found nothing.
Create a free account to get started. Paid plans unlock all tracks.