You can extract names and rank individual customers. Now your board wants plans ranked by total MRR contribution — which plan drives the most revenue?
After extract_customer_names I've been working with individual customers. But ranking plans — Pro contributes X, Starter contributes Y — means grouping first, then sorting the groups.
You already have group_by_plan and plan_summary from Week 2. Now add sorted(key=...) on the summary output. The pattern is: group → summarise → sort:
summary = plan_summary(customers)
ranked = sorted(
[{"plan": k, **v} for k, v in summary.items()],
key=lambda x: x["total_mrr"],
reverse=True
)What does {"plan": k, **v} mean? The double asterisk is new.
**v is dict unpacking — it spreads all key-value pairs from dict v into the new dict. So {"plan": k, **v} merges the plan name with the stats dict into one flat dict: {"plan": "pro", "total_mrr": 14850.0, "total_seats": 99, ...}. Here's the full function:
def rank_plans_by_mrr(customers: list) -> list:
summary = plan_summary(customers)
ranked = sorted(
[{"plan": k, "total_mrr": v["total_mrr"], "customer_count": v["total_seats"]} for k, v in summary.items()],
key=lambda x: x["total_mrr"],
reverse=True
)
print(f"Ranked {len(ranked)} plans")
return rankedSo rank_plans_by_mrr gives me a sorted list — highest MRR plan first — I can loop over and call make_update_line on each one. That's the full investor update structure.
The board deck's 'Plan Breakdown' slide just became a function call.
I composed group_by_plan, plan_summary, and sorted into one ranked output. Building on earlier functions is the whole game.
sorted() always returns a new list — it never modifies the original. If you need to sort in place, use .sort() on the list directly. For most cases, sorted() is safer because it doesn't mutate the input.
sorted(items, key=fn, reverse=True) sorts by a key function.
sorted(plans, key=lambda x: x['total_mrr'], reverse=True)| Pattern | Effect |
|---|---|
key=lambda x: x['field'] | Sort by dict field |
reverse=True | Descending (largest first) |
reverse=False | Ascending (default) |
sorted() | Returns new list |
.sort() | Sorts in place, returns None |
reverse=True for descending. Forgetting it sorts ascending — your top plan appears last.
You can extract names and rank individual customers. Now your board wants plans ranked by total MRR contribution — which plan drives the most revenue?
After extract_customer_names I've been working with individual customers. But ranking plans — Pro contributes X, Starter contributes Y — means grouping first, then sorting the groups.
You already have group_by_plan and plan_summary from Week 2. Now add sorted(key=...) on the summary output. The pattern is: group → summarise → sort:
summary = plan_summary(customers)
ranked = sorted(
[{"plan": k, **v} for k, v in summary.items()],
key=lambda x: x["total_mrr"],
reverse=True
)What does {"plan": k, **v} mean? The double asterisk is new.
**v is dict unpacking — it spreads all key-value pairs from dict v into the new dict. So {"plan": k, **v} merges the plan name with the stats dict into one flat dict: {"plan": "pro", "total_mrr": 14850.0, "total_seats": 99, ...}. Here's the full function:
def rank_plans_by_mrr(customers: list) -> list:
summary = plan_summary(customers)
ranked = sorted(
[{"plan": k, "total_mrr": v["total_mrr"], "customer_count": v["total_seats"]} for k, v in summary.items()],
key=lambda x: x["total_mrr"],
reverse=True
)
print(f"Ranked {len(ranked)} plans")
return rankedSo rank_plans_by_mrr gives me a sorted list — highest MRR plan first — I can loop over and call make_update_line on each one. That's the full investor update structure.
The board deck's 'Plan Breakdown' slide just became a function call.
I composed group_by_plan, plan_summary, and sorted into one ranked output. Building on earlier functions is the whole game.
sorted() always returns a new list — it never modifies the original. If you need to sort in place, use .sort() on the list directly. For most cases, sorted() is safer because it doesn't mutate the input.
sorted(items, key=fn, reverse=True) sorts by a key function.
sorted(plans, key=lambda x: x['total_mrr'], reverse=True)| Pattern | Effect |
|---|---|
key=lambda x: x['field'] | Sort by dict field |
reverse=True | Descending (largest first) |
reverse=False | Ascending (default) |
sorted() | Returns new list |
.sort() | Sorts in place, returns None |
reverse=True for descending. Forgetting it sorts ascending — your top plan appears last.
Ava needs a ranked plan breakdown for her board deck — which plan contributes the most MRR? Write `rank_plans_by_mrr(customers)` that groups customers by plan, computes total MRR per plan, and returns a list of `{'plan': ..., 'total_mrr': ..., 'customer_count': ...}` dicts sorted by total_mrr descending.
Tap each step for scaffolded hints.
No blank-editor panic.