You have a list of transaction dicts and you want them ordered from biggest amount to smallest. Python has sorted() — but how does it know which field to sort by?
I need to tell it to use t["amount"] as the sort value, not the whole dict?
Exactly. sorted() takes a key= argument — a tiny function that extracts the sort value from each element. The cleanest way to write that tiny function is with lambda:
sorted(txns, key=lambda t: t["amount"])lambda t: t["amount"] is a function that takes t and returns t["amount"]. sorted() calls it on every element and sorts by the result.
That gives me smallest first. How do I flip it to biggest first?
Add reverse=True:
sorted(txns, key=lambda t: t["amount"], reverse=True)sorted() defaults to ascending. reverse=True reverses the output — largest at index 0.
Why sorted() and not .sort()? I've seen both.
Different promises. list.sort() modifies the list in place and returns None. sorted(list) returns a new sorted list and leaves the original alone. For a function that returns the sorted list, sorted() is the right choice — no hidden side effects on the caller's data.
So the function is one line — sorted() with a key and reverse=True. Same shape works for any field I want to sort on.
Once you know the key=lambda shape, sorting by any field on any object is one line. You'll use this every time you turn raw data into a ranked view.
sorted() with key=TL;DR: sorted(items, key=func, reverse=bool) — returns a new sorted list.
key= — a function that returns the value to sort bylambda t: t["amount"] — inline "extract the amount" functionreverse=True — descending ordersorted() vs .sort()| Call | Returns | Side effect |
|---|---|---|
sorted(xs) | new list | none |
xs.sort() | None | mutates xs |
Use sorted() when you want a new list back. Use .sort() when you deliberately want to mutate the existing list in place.
Write `sort_by_amount(txns)` that returns a new list of transaction dicts sorted by amount, biggest first.
Tap each step for scaffolded hints.
No blank-editor panic.
You have a list of transaction dicts and you want them ordered from biggest amount to smallest. Python has sorted() — but how does it know which field to sort by?
I need to tell it to use t["amount"] as the sort value, not the whole dict?
Exactly. sorted() takes a key= argument — a tiny function that extracts the sort value from each element. The cleanest way to write that tiny function is with lambda:
sorted(txns, key=lambda t: t["amount"])lambda t: t["amount"] is a function that takes t and returns t["amount"]. sorted() calls it on every element and sorts by the result.
That gives me smallest first. How do I flip it to biggest first?
Add reverse=True:
sorted(txns, key=lambda t: t["amount"], reverse=True)sorted() defaults to ascending. reverse=True reverses the output — largest at index 0.
Why sorted() and not .sort()? I've seen both.
Different promises. list.sort() modifies the list in place and returns None. sorted(list) returns a new sorted list and leaves the original alone. For a function that returns the sorted list, sorted() is the right choice — no hidden side effects on the caller's data.
So the function is one line — sorted() with a key and reverse=True. Same shape works for any field I want to sort on.
Once you know the key=lambda shape, sorting by any field on any object is one line. You'll use this every time you turn raw data into a ranked view.
sorted() with key=TL;DR: sorted(items, key=func, reverse=bool) — returns a new sorted list.
key= — a function that returns the value to sort bylambda t: t["amount"] — inline "extract the amount" functionreverse=True — descending ordersorted() vs .sort()| Call | Returns | Side effect |
|---|---|---|
sorted(xs) | new list | none |
xs.sort() | None | mutates xs |
Use sorted() when you want a new list back. Use .sort() when you deliberately want to mutate the existing list in place.