You have URLs. You also have titles. Often you want both on one line — something a human can read at a glance, like "Introducing Python 3.13: https://python.org/blog/...". How do you combine two fields per result into one string?
An f-string inside a comprehension? Like [f"{r['title']}: {r['url']}" for r in results]?
Exactly. The f-string template goes where the expression lives; Python evaluates it per row. Two fields, one line, one step:
results = search("python 3.13 release", count=3)
lines = [f"{r['title']}: {r['url']}" for r in results]
print(lines)Notice the quote juggle — the outer f-string uses double quotes, the inner dict key uses single quotes. Mix them and Python reads cleanly.
So every element of the output list is a formatted string, not a dict? The shape changed from list[dict] to list[str] in one expression.
That is the power of comprehensions — they don't just filter or map, they reshape. You can go from list[dict] to list[str], or list[str] to list[int], in one line. Here's the function wrapped up:
def format_search_results(query: str, count: int) -> list:
results = search(query, count=count)
return [f"{r['title']}: {r['url']}" for r in results]What if a title has a colon in it? Would that confuse downstream code that tries to split on :?
Possibly, if the downstream code does line.split(':') expecting a clean split. That is a data-hygiene problem you handle at the consumer — maybe line.rsplit(':', 1) splits only on the last colon, leaving title colons intact. For this lesson, the output is for human reading, not parsing — so the formatting is the goal, not a round-trip encoding.
And the output is exactly what Leo-style reports or Slack messages need — one scannable line per result, no JSON to mentally parse.
Exactly. The format is the product. Give a human a list of 10 clean one-liners and they read it in 5 seconds. Give them 10 raw dicts and they spend 30 seconds decoding brackets. Same information, very different ergonomics.
TL;DR: [f"...{r['key']}..." for r in items] produces a list of formatted strings in one line.
f"", inner '', or vice versalist[dict] → list[str] in one comprehension| Template | Output |
|---|---|
f"{r['title']}: {r['url']}" | "Title: https://..." |
f"- {r['title']}" | "- Title" (Markdown bullet) |
f"{i+1}. {r['title']}" | numbered list — use enumerate |
For the numbered list, swap for r in results for for i, r in enumerate(results).
You have URLs. You also have titles. Often you want both on one line — something a human can read at a glance, like "Introducing Python 3.13: https://python.org/blog/...". How do you combine two fields per result into one string?
An f-string inside a comprehension? Like [f"{r['title']}: {r['url']}" for r in results]?
Exactly. The f-string template goes where the expression lives; Python evaluates it per row. Two fields, one line, one step:
results = search("python 3.13 release", count=3)
lines = [f"{r['title']}: {r['url']}" for r in results]
print(lines)Notice the quote juggle — the outer f-string uses double quotes, the inner dict key uses single quotes. Mix them and Python reads cleanly.
So every element of the output list is a formatted string, not a dict? The shape changed from list[dict] to list[str] in one expression.
That is the power of comprehensions — they don't just filter or map, they reshape. You can go from list[dict] to list[str], or list[str] to list[int], in one line. Here's the function wrapped up:
def format_search_results(query: str, count: int) -> list:
results = search(query, count=count)
return [f"{r['title']}: {r['url']}" for r in results]What if a title has a colon in it? Would that confuse downstream code that tries to split on :?
Possibly, if the downstream code does line.split(':') expecting a clean split. That is a data-hygiene problem you handle at the consumer — maybe line.rsplit(':', 1) splits only on the last colon, leaving title colons intact. For this lesson, the output is for human reading, not parsing — so the formatting is the goal, not a round-trip encoding.
And the output is exactly what Leo-style reports or Slack messages need — one scannable line per result, no JSON to mentally parse.
Exactly. The format is the product. Give a human a list of 10 clean one-liners and they read it in 5 seconds. Give them 10 raw dicts and they spend 30 seconds decoding brackets. Same information, very different ergonomics.
TL;DR: [f"...{r['key']}..." for r in items] produces a list of formatted strings in one line.
f"", inner '', or vice versalist[dict] → list[str] in one comprehension| Template | Output |
|---|---|
f"{r['title']}: {r['url']}" | "Title: https://..." |
f"- {r['title']}" | "- Title" (Markdown bullet) |
f"{i+1}. {r['title']}" | numbered list — use enumerate |
For the numbered list, swap for r in results for for i, r in enumerate(results).
Create a free account to get started. Paid plans unlock all tracks.