You sent the same question to the agent six different ways and want the most concise answer. How would Python pick it out of a list of strings?
Sort by length and take the first? Or check each one with a length variable?
Both work, but Python has a cleaner tool. min(iterable, key=fn) finds the item where fn(item) is smallest. With key=len it returns the shortest string directly — no sort, no manual loop:
words = ["elephant", "cat", "hippopotamus", "ox"]
print(min(words, key=len)) # "ox"Notice you pass len itself, without parentheses. Python applies it to each item internally and returns the winner.
len without parens? Why not len()?
len() with no argument raises TypeError. When you write key=len, you hand Python the function object and it calls it for you — once per item. Same pattern works in max, sorted, and list.sort. And since agent outputs are ordinary strings, the exact same reduction applies to them:
def shortest_response(prompts: list) -> str:
agent = Agent(model)
outputs = [agent.run_sync(p).output for p in prompts]
return min(outputs, key=len)Why would you ever want the shortest answer? Isn't more information usually better?
Not always. UI components have character limits — notifications, button labels, tweets. Short responses also correlate with confidence — a hedge-heavy five-paragraph answer often means the model is unsure. And "shortest" is only one criterion; swap min for max or sorted and you get a different kind of "best" — all with the same key=len argument.
So AI outputs are just ordinary Python strings — once I have them in a list, every list function I know applies?
That is the unlock of this week. The model does the hard work; standard Python handles everything after. Write shortest_response(prompts) now: batch the prompts through one agent, then min(outputs, key=len).
TL;DR: min(outputs, key=len) returns the shortest string in a list.
min(iterable, key=fn) — returns the item scoring lowest by fn(item)key=len — passes the function object, no parensmax, sorted — pick the criterion that fits| Need | Key function |
|---|---|
| Shortest | key=len |
| Most detailed | max(..., key=len) |
| Ranked | sorted(..., key=len) |
Agent outputs are strings — standard Python reductions apply without any changes.
You sent the same question to the agent six different ways and want the most concise answer. How would Python pick it out of a list of strings?
Sort by length and take the first? Or check each one with a length variable?
Both work, but Python has a cleaner tool. min(iterable, key=fn) finds the item where fn(item) is smallest. With key=len it returns the shortest string directly — no sort, no manual loop:
words = ["elephant", "cat", "hippopotamus", "ox"]
print(min(words, key=len)) # "ox"Notice you pass len itself, without parentheses. Python applies it to each item internally and returns the winner.
len without parens? Why not len()?
len() with no argument raises TypeError. When you write key=len, you hand Python the function object and it calls it for you — once per item. Same pattern works in max, sorted, and list.sort. And since agent outputs are ordinary strings, the exact same reduction applies to them:
def shortest_response(prompts: list) -> str:
agent = Agent(model)
outputs = [agent.run_sync(p).output for p in prompts]
return min(outputs, key=len)Why would you ever want the shortest answer? Isn't more information usually better?
Not always. UI components have character limits — notifications, button labels, tweets. Short responses also correlate with confidence — a hedge-heavy five-paragraph answer often means the model is unsure. And "shortest" is only one criterion; swap min for max or sorted and you get a different kind of "best" — all with the same key=len argument.
So AI outputs are just ordinary Python strings — once I have them in a list, every list function I know applies?
That is the unlock of this week. The model does the hard work; standard Python handles everything after. Write shortest_response(prompts) now: batch the prompts through one agent, then min(outputs, key=len).
TL;DR: min(outputs, key=len) returns the shortest string in a list.
min(iterable, key=fn) — returns the item scoring lowest by fn(item)key=len — passes the function object, no parensmax, sorted — pick the criterion that fits| Need | Key function |
|---|---|
| Shortest | key=len |
| Most detailed | max(..., key=len) |
| Ranked | sorted(..., key=len) |
Agent outputs are strings — standard Python reductions apply without any changes.
Create a free account to get started. Paid plans unlock all tracks.