You have a list of ten queries, but three of them are duplicates. Naively, you'd make ten search() calls. What is the smallest structure that lets you only pay for the unique ones?
A dict? Use the query string as the key and the result URLs as the value — if the key is already there, skip the search.
Exactly. A local dict acts as a cache that lives for the duration of the function call. Check before fetching; fetch only on miss:
cache = {}
for q in queries:
if q not in cache:
results = search(q, count=3)
cache[q] = [r["url"] for r in results]
print(cache)Ten queries in, three unique keys out — seven duplicate calls saved.
So the cache doesn't have to be fancy — a plain dict works because the query string is already hashable?
Exactly. Strings are hashable; dicts use them as keys; the lookup is O(1). No cache library, no Redis, no TTL. For within-function deduplication this is the entire pattern:
def cache_search_queries(queries: list) -> dict:
cache = {}
for q in queries:
if q not in cache:
results = search(q, count=3)
cache[q] = [r["url"] for r in results]
return cacheThe output dict maps query -> list of URLs, with every duplicate query silently skipping the API call.
What about the unique-query order? If I give you ['a', 'b', 'a', 'c'], is the cache {a, b, c} or {a, c, b}?
{a, b, c} — insertion order. Python dicts preserve insertion order since 3.7, so the cache's keys reflect the order queries first appeared. That matters if you later iterate over cache.items() and want the output to match the original list's first appearances.
So three API calls for ten queries. The cache pays off immediately — the more duplicates in the input, the bigger the speedup.
Exactly. Caching is just a dict and a not-in check. The same shape scales to expensive agent calls too — if key not in cache is the universal pattern, whatever sits behind the colon.
TL;DR: check if key not in cache before calling; store the result on the miss path.
in on a dict is constant time| Step | Code |
|---|---|
| Lookup | if q not in cache |
| Fetch | search(q, count=3) |
| Store | cache[q] = ... |
Skip libraries for in-function dedup — a dict and a conditional is the pattern.
You have a list of ten queries, but three of them are duplicates. Naively, you'd make ten search() calls. What is the smallest structure that lets you only pay for the unique ones?
A dict? Use the query string as the key and the result URLs as the value — if the key is already there, skip the search.
Exactly. A local dict acts as a cache that lives for the duration of the function call. Check before fetching; fetch only on miss:
cache = {}
for q in queries:
if q not in cache:
results = search(q, count=3)
cache[q] = [r["url"] for r in results]
print(cache)Ten queries in, three unique keys out — seven duplicate calls saved.
So the cache doesn't have to be fancy — a plain dict works because the query string is already hashable?
Exactly. Strings are hashable; dicts use them as keys; the lookup is O(1). No cache library, no Redis, no TTL. For within-function deduplication this is the entire pattern:
def cache_search_queries(queries: list) -> dict:
cache = {}
for q in queries:
if q not in cache:
results = search(q, count=3)
cache[q] = [r["url"] for r in results]
return cacheThe output dict maps query -> list of URLs, with every duplicate query silently skipping the API call.
What about the unique-query order? If I give you ['a', 'b', 'a', 'c'], is the cache {a, b, c} or {a, c, b}?
{a, b, c} — insertion order. Python dicts preserve insertion order since 3.7, so the cache's keys reflect the order queries first appeared. That matters if you later iterate over cache.items() and want the output to match the original list's first appearances.
So three API calls for ten queries. The cache pays off immediately — the more duplicates in the input, the bigger the speedup.
Exactly. Caching is just a dict and a not-in check. The same shape scales to expensive agent calls too — if key not in cache is the universal pattern, whatever sits behind the colon.
TL;DR: check if key not in cache before calling; store the result on the miss path.
in on a dict is constant time| Step | Code |
|---|---|
| Lookup | if q not in cache |
| Fetch | search(q, count=3) |
| Store | cache[q] = ... |
Skip libraries for in-function dedup — a dict and a conditional is the pattern.
Create a free account to get started. Paid plans unlock all tracks.