You want to ask the live web a question and find out how many results come back. What is the smallest Python program that can do that?
I've seen search(...) mentioned, but I don't know the shape of what it returns. A string? A JSON blob I have to unwrap? A response object with a status code?
A plain list of dicts. search(query, count=n) runs a live web query through Brave Search and returns a list where each item has title, url, and snippet keys. Nothing nested, nothing to unwrap:
results = search("python tutorials", count=3)
print(len(results)) # 3So if I want a count, I just call len() on the list. That is really the whole function?
That is the whole function. search() handles authentication, the HTTP request, JSON parsing, and error handling inside. You own the logic. Two lines is the right answer:
def search_count_results(query: str, count: int) -> int:
results = search(query, count=count)
return len(results)What if I ask for ten results but only three exist for a super-niche query? Do I still get ten back, padded somehow?
You get however many the engine actually found — the list may be shorter than count. That is why returning len(results) is more honest than returning count directly. You are counting what came back, not what you asked for. That distinction matters the moment you build dashboards on top of this function.
So the print shows the live answer to my query — a real number from the real web, not a canned one?
Every run hits the live search index. Two calls with the same query seconds apart can return the same count or different ones, depending on what the engine indexed. This is your first taste of real-data feedback — the rest of Week 1 builds on it.
TL;DR: search(query, count=n) returns a list of dicts; use len(results) for the actual count.
await needed{"title": str, "url": str, "snippet": str}count is a max — the list may be shorter| Scenario | len(results) |
|---|---|
| Popular topic | often equals count |
| Niche query | often less than count |
| Bad network | can raise an exception |
Returning len(results) is always more accurate than returning count.
You want to ask the live web a question and find out how many results come back. What is the smallest Python program that can do that?
I've seen search(...) mentioned, but I don't know the shape of what it returns. A string? A JSON blob I have to unwrap? A response object with a status code?
A plain list of dicts. search(query, count=n) runs a live web query through Brave Search and returns a list where each item has title, url, and snippet keys. Nothing nested, nothing to unwrap:
results = search("python tutorials", count=3)
print(len(results)) # 3So if I want a count, I just call len() on the list. That is really the whole function?
That is the whole function. search() handles authentication, the HTTP request, JSON parsing, and error handling inside. You own the logic. Two lines is the right answer:
def search_count_results(query: str, count: int) -> int:
results = search(query, count=count)
return len(results)What if I ask for ten results but only three exist for a super-niche query? Do I still get ten back, padded somehow?
You get however many the engine actually found — the list may be shorter than count. That is why returning len(results) is more honest than returning count directly. You are counting what came back, not what you asked for. That distinction matters the moment you build dashboards on top of this function.
So the print shows the live answer to my query — a real number from the real web, not a canned one?
Every run hits the live search index. Two calls with the same query seconds apart can return the same count or different ones, depending on what the engine indexed. This is your first taste of real-data feedback — the rest of Week 1 builds on it.
TL;DR: search(query, count=n) returns a list of dicts; use len(results) for the actual count.
await needed{"title": str, "url": str, "snippet": str}count is a max — the list may be shorter| Scenario | len(results) |
|---|---|
| Popular topic | often equals count |
| Niche query | often less than count |
| Bad network | can raise an exception |
Returning len(results) is always more accurate than returning count.
Create a free account to get started. Paid plans unlock all tracks.