You've been building the weekly investor update piece by piece. Now imagine your investor asks: 'Who are your top 5 customers by MRR?' How do you answer that today?
After customers_to_json I can export the summary. But ranking individual customers — not plans — means going back to the raw list, sorting it, taking the top 5. That's usually a fresh pivot.
A list comprehension collapses the filter into one line, then sorted() with key= handles the ranking. No loop, no append, no intermediate list:
valid = [c for c in customers if c.get('mrr', 0) > 0]
top = sorted(valid, key=lambda x: x.get('mrr', 0), reverse=True)[:n]lambda x: x.get('mrr', 0) — what's a lambda?
A lambda is an anonymous function — one expression, no def, no name. lambda x: x.get('mrr', 0) is a function that takes a customer dict and returns its MRR. You pass it to sorted(key=...) so Python knows which field to sort by. Here's the full function:
def top_customers_by_mrr(customers: list, n: int = 5) -> list:
valid = [c for c in customers if c.get("mrr", 0) > 0]
top = sorted(valid, key=lambda x: x.get("mrr", 0), reverse=True)[:n]
print(f"Top {len(top)} customers by MRR")
return topSo top_customers_by_mrr(customers, 3) gives my top 3 in one call. I can feed that list directly into make_update_line from Week 3.
Your investor's 'who are the top 5?' email just became a function call. The query that used to take a pivot is now a parameter.
I went from a filter to a ranked list in two lines. This is what composable functions feel like.
reverse=True sorts descending — largest first. Without it, sorted() sorts ascending. The [:n] slice takes the first N items from the sorted list — Python slice syntax, same as strings.
A list comprehension builds a new list in one expression. For investor updates, filtering and ranking the top customers in a single call means the board-meeting answer is always one line away.
[expression for item in iterable if condition]# for loop
results = []
for c in customers:
if c.get('mrr', 0) > 0:
results.append(c)
# comprehension (same result)
results = [c for c in customers if c.get('mrr', 0) > 0]sorted(items, key=lambda x: x['field'], reverse=True) sorts by a field, descending.
You've been building the weekly investor update piece by piece. Now imagine your investor asks: 'Who are your top 5 customers by MRR?' How do you answer that today?
After customers_to_json I can export the summary. But ranking individual customers — not plans — means going back to the raw list, sorting it, taking the top 5. That's usually a fresh pivot.
A list comprehension collapses the filter into one line, then sorted() with key= handles the ranking. No loop, no append, no intermediate list:
valid = [c for c in customers if c.get('mrr', 0) > 0]
top = sorted(valid, key=lambda x: x.get('mrr', 0), reverse=True)[:n]lambda x: x.get('mrr', 0) — what's a lambda?
A lambda is an anonymous function — one expression, no def, no name. lambda x: x.get('mrr', 0) is a function that takes a customer dict and returns its MRR. You pass it to sorted(key=...) so Python knows which field to sort by. Here's the full function:
def top_customers_by_mrr(customers: list, n: int = 5) -> list:
valid = [c for c in customers if c.get("mrr", 0) > 0]
top = sorted(valid, key=lambda x: x.get("mrr", 0), reverse=True)[:n]
print(f"Top {len(top)} customers by MRR")
return topSo top_customers_by_mrr(customers, 3) gives my top 3 in one call. I can feed that list directly into make_update_line from Week 3.
Your investor's 'who are the top 5?' email just became a function call. The query that used to take a pivot is now a parameter.
I went from a filter to a ranked list in two lines. This is what composable functions feel like.
reverse=True sorts descending — largest first. Without it, sorted() sorts ascending. The [:n] slice takes the first N items from the sorted list — Python slice syntax, same as strings.
A list comprehension builds a new list in one expression. For investor updates, filtering and ranking the top customers in a single call means the board-meeting answer is always one line away.
[expression for item in iterable if condition]# for loop
results = []
for c in customers:
if c.get('mrr', 0) > 0:
results.append(c)
# comprehension (same result)
results = [c for c in customers if c.get('mrr', 0) > 0]sorted(items, key=lambda x: x['field'], reverse=True) sorts by a field, descending.
Leila's investors ask for the top 5 customers by MRR at the start of every board meeting. Write `top_customers_by_mrr(customers, n=5)` that uses a list comprehension to filter customers with positive MRR, sorts descending by MRR, and returns the top N customer dicts.
Tap each step for scaffolded hints.
No blank-editor panic.