You have a list of product dicts, each with a name and a price. You want them in ascending price order. How does sorted() know which field to compare on?
I've used sorted() on a list of numbers, but never on dicts. What happens if I just pass the list directly?
Python would try to compare whole dicts, which raises a TypeError — dicts aren't orderable. You need to tell sorted() what to extract for comparison, via the key parameter. A lambda is the idiomatic way:
sorted(products, key=lambda p: p["price"])The lambda takes one product dict and returns its price — the value sorted() uses to rank.
So the lambda is a mini-function that runs once per item, just to pull out the sort key?
Exactly. lambda p: p["price"] is shorthand for def get_price(p): return p["price"] — inline, anonymous, single-use. For simple field extraction, lambdas keep the code tight:
def sort_by_price(products: list) -> list:
return sorted(products, key=lambda p: p["price"])Why sorted() instead of calling .sort() on the list directly?
sorted() returns a new list — the original is untouched. list.sort() mutates the input and returns None. For a function that takes input and returns output, sorted() is always the right choice. Accidentally writing return products.sort(...) returns None, which is a subtle bug that breaks silently.
So the full move is sorted(), key=lambda to extract the field, and it handles the empty list and ties automatically?
That's the whole move. Ascending by default, stable on ties (original order preserved among equal keys), empty in means empty out. Change the lambda to extract any other field and you have a totally general sort.
TL;DR: sorted(items, key=lambda x: x["field"]) sorts by a specific field without mutating the input.
key=fn — function called once per item to extract the sort valuelambda p: p["price"] — inline anonymous function for field extractionsorted vs .sort — sorted returns new; .sort mutates + returns None| Goal | Call |
|---|---|
| Ascending by price | sorted(xs, key=lambda x: x["price"]) |
| Descending by price | sorted(xs, key=lambda x: x["price"], reverse=True) |
| By nested field | sorted(xs, key=lambda x: x["a"]["b"]) |
The lambda is the contract: "here's how to turn one item into its sort key."
You have a list of product dicts, each with a name and a price. You want them in ascending price order. How does sorted() know which field to compare on?
I've used sorted() on a list of numbers, but never on dicts. What happens if I just pass the list directly?
Python would try to compare whole dicts, which raises a TypeError — dicts aren't orderable. You need to tell sorted() what to extract for comparison, via the key parameter. A lambda is the idiomatic way:
sorted(products, key=lambda p: p["price"])The lambda takes one product dict and returns its price — the value sorted() uses to rank.
So the lambda is a mini-function that runs once per item, just to pull out the sort key?
Exactly. lambda p: p["price"] is shorthand for def get_price(p): return p["price"] — inline, anonymous, single-use. For simple field extraction, lambdas keep the code tight:
def sort_by_price(products: list) -> list:
return sorted(products, key=lambda p: p["price"])Why sorted() instead of calling .sort() on the list directly?
sorted() returns a new list — the original is untouched. list.sort() mutates the input and returns None. For a function that takes input and returns output, sorted() is always the right choice. Accidentally writing return products.sort(...) returns None, which is a subtle bug that breaks silently.
So the full move is sorted(), key=lambda to extract the field, and it handles the empty list and ties automatically?
That's the whole move. Ascending by default, stable on ties (original order preserved among equal keys), empty in means empty out. Change the lambda to extract any other field and you have a totally general sort.
TL;DR: sorted(items, key=lambda x: x["field"]) sorts by a specific field without mutating the input.
key=fn — function called once per item to extract the sort valuelambda p: p["price"] — inline anonymous function for field extractionsorted vs .sort — sorted returns new; .sort mutates + returns None| Goal | Call |
|---|---|
| Ascending by price | sorted(xs, key=lambda x: x["price"]) |
| Descending by price | sorted(xs, key=lambda x: x["price"], reverse=True) |
| By nested field | sorted(xs, key=lambda x: x["a"]["b"]) |
The lambda is the contract: "here's how to turn one item into its sort key."
Write `sort_by_price(products)` that returns a new list of product dicts sorted by the `price` field in ascending order. Do not modify the input.
Tap each step for scaffolded hints.
No blank-editor panic.