clients_to_json from last week serialises the status summary. But which clients actually drove the most revenue this month? You need a ranked top-N list — filtered and sorted — in one clean expression.
A list comprehension for the filter, then sorted with a key function for the ranking. Both patterns from Week 2.
Exactly. A list comprehension filters out clients with zero hours in one line: [c for c in clients if c.get("hours", 0) > 0]. Then sorted() with a key function ranks by revenue — highest first:
valid = [c for c in clients if c.get("hours", 0) > 0]
ranked = sorted(valid, key=lambda c: c.get("rate", 0) * c.get("hours", 0), reverse=True)
top_n = ranked[:n]What is lambda? I have seen it but never used it.
lambda c: c.get("rate", 0) * c.get("hours", 0) is an anonymous function — a function without a def. sorted(key=lambda c: ...) calls that expression on each item to decide the sort order. You can replace it with a named function, but lambda is compact for single-expression sort keys.
So top_clients_by_revenue(clients, 3) gives me the three highest-revenue clients this month. My top-client dashboard in one call.
One comprehension. One sort. One slice. The ranked client list you used to build by sorting a spreadsheet:
def top_clients_by_revenue(clients: list, n: int = 5) -> list:
valid = [c for c in clients if c.get("hours", 0) > 0]
ranked = sorted(valid, key=lambda c: c.get("rate", 0) * c.get("hours", 0), reverse=True)
top_n = ranked[:n]
print(f"Top {len(top_n)} clients by revenue")
return top_nMy Monday morning revenue review went from fifteen minutes to one function call.
What would you add to rank by effective rate instead of total revenue?
valid = [c for c in clients if c.get('hours', 0) > 0].get('hours', 0) returns 0 if the key is missing — safer than c['hours'] which raises KeyError. Filtering out zero-hour clients avoids a divide-by-zero in revenue calculations.
Sorting with sorted(key=...):
sorted(valid, key=lambda c: c['rate'] * c['hours'], reverse=True)[:n]The lambda computes revenue inline for each client. reverse=True puts the highest-revenue client first. [:n] slices the top N entries from the sorted list.
clients_to_json from last week serialises the status summary. But which clients actually drove the most revenue this month? You need a ranked top-N list — filtered and sorted — in one clean expression.
A list comprehension for the filter, then sorted with a key function for the ranking. Both patterns from Week 2.
Exactly. A list comprehension filters out clients with zero hours in one line: [c for c in clients if c.get("hours", 0) > 0]. Then sorted() with a key function ranks by revenue — highest first:
valid = [c for c in clients if c.get("hours", 0) > 0]
ranked = sorted(valid, key=lambda c: c.get("rate", 0) * c.get("hours", 0), reverse=True)
top_n = ranked[:n]What is lambda? I have seen it but never used it.
lambda c: c.get("rate", 0) * c.get("hours", 0) is an anonymous function — a function without a def. sorted(key=lambda c: ...) calls that expression on each item to decide the sort order. You can replace it with a named function, but lambda is compact for single-expression sort keys.
So top_clients_by_revenue(clients, 3) gives me the three highest-revenue clients this month. My top-client dashboard in one call.
One comprehension. One sort. One slice. The ranked client list you used to build by sorting a spreadsheet:
def top_clients_by_revenue(clients: list, n: int = 5) -> list:
valid = [c for c in clients if c.get("hours", 0) > 0]
ranked = sorted(valid, key=lambda c: c.get("rate", 0) * c.get("hours", 0), reverse=True)
top_n = ranked[:n]
print(f"Top {len(top_n)} clients by revenue")
return top_nMy Monday morning revenue review went from fifteen minutes to one function call.
What would you add to rank by effective rate instead of total revenue?
valid = [c for c in clients if c.get('hours', 0) > 0].get('hours', 0) returns 0 if the key is missing — safer than c['hours'] which raises KeyError. Filtering out zero-hour clients avoids a divide-by-zero in revenue calculations.
Sorting with sorted(key=...):
sorted(valid, key=lambda c: c['rate'] * c['hours'], reverse=True)[:n]The lambda computes revenue inline for each client. reverse=True puts the highest-revenue client first. [:n] slices the top N entries from the sorted list.
Kai wants to know which clients drove the most revenue this month so she can prioritise them in her quarterly review. Write `top_clients_by_revenue(clients, n=5)` that filters out clients with zero hours, then returns the top `n` client dicts ranked by total revenue (rate × hours, descending). Each dict has `name`, `rate`, and `hours` keys.
Tap each step for scaffolded hints.
No blank-editor panic.