status_summary from Week 2 gives you total revenue per status group. But the monthly report needs the groups ranked — highest revenue status first. Which Python function turns an unsorted dict into a ranked list?
sorted() with a key. I used it yesterday on individual clients. Now I apply it to the groups dict.
Exactly. .items() gives you (status, stats_dict) pairs. sorted(..., key=lambda item: item[1]["total_revenue"], reverse=True) ranks by revenue descending. The result is a list of tuples you can format into a report:
groups = status_summary(clients)
ranked = sorted(groups.items(), key=lambda item: item[1]["total_revenue"], reverse=True)
for status, stats in ranked:
print(f"{status}: ${stats['total_revenue']:.2f}")item[1]["total_revenue"] — why [1]? What is item[0]?
.items() returns (key, value) tuples. item[0] is the status string — "on track", "critical". item[1] is the stats dict — {"total_hours": ..., "total_revenue": ...}. You sort by the revenue inside the stats dict, so you reach through item[1] to get there.
So the output is a list of {"status": ..., "total_revenue": ..., "total_hours": ...} dicts I can pass to the capstone.
Clean the data, group it, rank it, report it. The invoice generator capstone is one assembly step away:
def rank_clients_by_revenue(clients: list) -> list:
groups = status_summary(clients)
ranked = sorted(groups.items(), key=lambda item: item[1].get("total_revenue", 0), reverse=True)
result = [{"status": s, "total_revenue": d.get("total_revenue", 0), "total_hours": d.get("total_hours", 0)} for s, d in ranked]
print(f"Ranked {len(result)} groups")
return resultMy quarterly ranking spreadsheet — gone. One function, correct order, every time.
One edge case: sorted() is stable. When two groups have equal revenue, their original order is preserved — which is the dict insertion order in Python 3.7+. Predictable, but worth knowing when you compare results.
sorted with key=lambdasorted(iterable, key=..., reverse=True) returns a new sorted list:
ranked = sorted(summary_list, key=lambda x: x['total_revenue'], reverse=True)lambda x: x['total_revenue'] extracts the sort value from each dict. reverse=True puts the highest revenue first.
Why sort a summary, not raw clients? group_by_status + status_summary computes total revenue per status group. Sorting those aggregated dicts gives a ranked view of where revenue is concentrated — more actionable than ranking individual clients.
Return shape: a list of {'status': ..., 'total_revenue': ..., 'total_hours': ...} dicts, ordered by revenue descending.
status_summary from Week 2 gives you total revenue per status group. But the monthly report needs the groups ranked — highest revenue status first. Which Python function turns an unsorted dict into a ranked list?
sorted() with a key. I used it yesterday on individual clients. Now I apply it to the groups dict.
Exactly. .items() gives you (status, stats_dict) pairs. sorted(..., key=lambda item: item[1]["total_revenue"], reverse=True) ranks by revenue descending. The result is a list of tuples you can format into a report:
groups = status_summary(clients)
ranked = sorted(groups.items(), key=lambda item: item[1]["total_revenue"], reverse=True)
for status, stats in ranked:
print(f"{status}: ${stats['total_revenue']:.2f}")item[1]["total_revenue"] — why [1]? What is item[0]?
.items() returns (key, value) tuples. item[0] is the status string — "on track", "critical". item[1] is the stats dict — {"total_hours": ..., "total_revenue": ...}. You sort by the revenue inside the stats dict, so you reach through item[1] to get there.
So the output is a list of {"status": ..., "total_revenue": ..., "total_hours": ...} dicts I can pass to the capstone.
Clean the data, group it, rank it, report it. The invoice generator capstone is one assembly step away:
def rank_clients_by_revenue(clients: list) -> list:
groups = status_summary(clients)
ranked = sorted(groups.items(), key=lambda item: item[1].get("total_revenue", 0), reverse=True)
result = [{"status": s, "total_revenue": d.get("total_revenue", 0), "total_hours": d.get("total_hours", 0)} for s, d in ranked]
print(f"Ranked {len(result)} groups")
return resultMy quarterly ranking spreadsheet — gone. One function, correct order, every time.
One edge case: sorted() is stable. When two groups have equal revenue, their original order is preserved — which is the dict insertion order in Python 3.7+. Predictable, but worth knowing when you compare results.
sorted with key=lambdasorted(iterable, key=..., reverse=True) returns a new sorted list:
ranked = sorted(summary_list, key=lambda x: x['total_revenue'], reverse=True)lambda x: x['total_revenue'] extracts the sort value from each dict. reverse=True puts the highest revenue first.
Why sort a summary, not raw clients? group_by_status + status_summary computes total revenue per status group. Sorting those aggregated dicts gives a ranked view of where revenue is concentrated — more actionable than ranking individual clients.
Return shape: a list of {'status': ..., 'total_revenue': ..., 'total_hours': ...} dicts, ordered by revenue descending.
Daniel needs his monthly project groups ranked by total revenue — highest-revenue status first — for the board summary. Write `rank_clients_by_revenue(clients)` that calls `status_summary(clients)`, sorts the groups by `total_revenue` descending, and returns a list of dicts with `status`, `total_revenue`, and `total_hours` keys.
Tap each step for scaffolded hints.
No blank-editor panic.