You have the top snippet. Now you want every URL from every result — ten results, ten URLs, in order. How do you turn a list of dicts into a list of strings in one expression?
A for loop that appends to an empty list? Or does Python have a shortcut for this pattern?
A list comprehension. It is the shortcut you reach for any time you want to transform every element of a list into something else. The full pattern reads left to right like "for each r in results, keep r['url']":
results = search("python testing", count=3)
urls = [r["url"] for r in results]
print(urls)One line, and urls is a list of strings in the same order as results.
So the same pattern works for any key — I could get all titles with [r["title"] for r in results], or all snippets the same way?
Exactly. The comprehension is the pattern; the key is the variable. Once you see it you never write the loop-and-append version again:
def extract_all_urls(query: str, count: int) -> list:
results = search(query, count=count)
return [r["url"] for r in results]What happens with the variable name r? Could I use anything, or is r special?
Anything. r is just a local name bound to each dict in turn. for result in results is clearer; for r in results is shorter. Both are right. The convention in short comprehensions is a one-letter name because the whole expression fits on one line. You could just as well write [hit["url"] for hit in results] and it would behave identically.
And because the output is still a list, I can chain it — pass it to len(), feed it into a set, iterate over it again.
Exactly. Comprehensions are building blocks. Week 2 uses the same shape — transform each search result — except the transform becomes an agent call instead of a dict lookup. Master the shape now and Week 2 is a rename away.
TL;DR: [expr for item in iterable] builds a new list by transforming each element.
expr — the value you want for each item (e.g., r["url"])item — local name for each element (r, result, anything)iterable — list, tuple, generator — anything you can loop over| Loop + append | Comprehension |
|---|---|
| 4 lines | 1 line |
| Mutates a list | Returns a list |
| Easy to miss a typo | Harder to read when nested |
For a straight list → list transform, the comprehension wins. Save the loop for multi-step transformations.
You have the top snippet. Now you want every URL from every result — ten results, ten URLs, in order. How do you turn a list of dicts into a list of strings in one expression?
A for loop that appends to an empty list? Or does Python have a shortcut for this pattern?
A list comprehension. It is the shortcut you reach for any time you want to transform every element of a list into something else. The full pattern reads left to right like "for each r in results, keep r['url']":
results = search("python testing", count=3)
urls = [r["url"] for r in results]
print(urls)One line, and urls is a list of strings in the same order as results.
So the same pattern works for any key — I could get all titles with [r["title"] for r in results], or all snippets the same way?
Exactly. The comprehension is the pattern; the key is the variable. Once you see it you never write the loop-and-append version again:
def extract_all_urls(query: str, count: int) -> list:
results = search(query, count=count)
return [r["url"] for r in results]What happens with the variable name r? Could I use anything, or is r special?
Anything. r is just a local name bound to each dict in turn. for result in results is clearer; for r in results is shorter. Both are right. The convention in short comprehensions is a one-letter name because the whole expression fits on one line. You could just as well write [hit["url"] for hit in results] and it would behave identically.
And because the output is still a list, I can chain it — pass it to len(), feed it into a set, iterate over it again.
Exactly. Comprehensions are building blocks. Week 2 uses the same shape — transform each search result — except the transform becomes an agent call instead of a dict lookup. Master the shape now and Week 2 is a rename away.
TL;DR: [expr for item in iterable] builds a new list by transforming each element.
expr — the value you want for each item (e.g., r["url"])item — local name for each element (r, result, anything)iterable — list, tuple, generator — anything you can loop over| Loop + append | Comprehension |
|---|---|
| 4 lines | 1 line |
| Mutates a list | Returns a list |
| Easy to miss a typo | Harder to read when nested |
For a straight list → list transform, the comprehension wins. Save the loop for multi-step transformations.
Create a free account to get started. Paid plans unlock all tracks.