You want the top 3 items from a list of dicts, ranked by score — highest first. Two moves on top of yesterday's sort: reverse the direction and slice to N. How do you wire them together?
sorted() with reverse=True for descending, then [:n] to take the first N?
Exactly. reverse=True is a keyword argument on sorted() — pass it alongside key=. Then chain a slice:
sorted(items, key=lambda x: x["score"], reverse=True)[:n]One expression. Sort descending, slice first N elements.
What if there are fewer than N items — say the list has 2 and I ask for the top 5?
Slicing never errors on an out-of-range index. [1, 2][:5] returns [1, 2] — you get as many as exist, up to N. No guard needed.
def top_n_by_score(items: list, n: int) -> list:
return sorted(items, key=lambda x: x["score"], reverse=True)[:n]The function handles any n, any list size, and any tie case — Python's sort is stable, so items with equal scores stay in input order.
What if n is zero? Does that just return an empty list?
[x, y, z][:0] is []. Zero-width slice. Same with negative n — [:-1] drops the last element. The slice operator is permissive; it never raises.
So sorted + reverse + slice is the universal top-N pattern? Any field, any collection.
That's the shape. Change the key lambda to extract any other field and you have a reusable top-N function. Leaderboards, top-priced items, most-recent posts — same three moves.
TL;DR: sorted(items, key=..., reverse=True)[:n] — three moves in one expression.
reverse=True — descending order (highest first)[:n] — take the first N; safe when list has fewer| Input | Behavior |
|---|---|
n > list length | returns all items |
n == 0 | returns [] |
| empty list | returns [] |
The slice is graceful — you never need an if len(items) < n check.
You want the top 3 items from a list of dicts, ranked by score — highest first. Two moves on top of yesterday's sort: reverse the direction and slice to N. How do you wire them together?
sorted() with reverse=True for descending, then [:n] to take the first N?
Exactly. reverse=True is a keyword argument on sorted() — pass it alongside key=. Then chain a slice:
sorted(items, key=lambda x: x["score"], reverse=True)[:n]One expression. Sort descending, slice first N elements.
What if there are fewer than N items — say the list has 2 and I ask for the top 5?
Slicing never errors on an out-of-range index. [1, 2][:5] returns [1, 2] — you get as many as exist, up to N. No guard needed.
def top_n_by_score(items: list, n: int) -> list:
return sorted(items, key=lambda x: x["score"], reverse=True)[:n]The function handles any n, any list size, and any tie case — Python's sort is stable, so items with equal scores stay in input order.
What if n is zero? Does that just return an empty list?
[x, y, z][:0] is []. Zero-width slice. Same with negative n — [:-1] drops the last element. The slice operator is permissive; it never raises.
So sorted + reverse + slice is the universal top-N pattern? Any field, any collection.
That's the shape. Change the key lambda to extract any other field and you have a reusable top-N function. Leaderboards, top-priced items, most-recent posts — same three moves.
TL;DR: sorted(items, key=..., reverse=True)[:n] — three moves in one expression.
reverse=True — descending order (highest first)[:n] — take the first N; safe when list has fewer| Input | Behavior |
|---|---|
n > list length | returns all items |
n == 0 | returns [] |
| empty list | returns [] |
The slice is graceful — you never need an if len(items) < n check.
Write `top_n_by_score(items, n)` that returns the top `n` items from `items` (a list of dicts with a `score` field) ranked by score in descending order. Return fewer items if the list is shorter than `n`.
Tap each step for scaffolded hints.
No blank-editor panic.