You can filter active customers now with filter_active_customers. But how do you compute ARPU for each one — MRR divided by seats?
In Excel I'd add a column with a formula: =B2/C2. But that's one cell at a time. For 500 rows I'd drag the formula down — except Python doesn't have a spreadsheet UI.
Python has something better. A for loop processes every row in milliseconds, and enumerate() gives you the position index if you need it. You build a new dict per customer with the computed ARPU, and append it to a results list:
for i, customer in enumerate(customers):
arpu = customer["mrr"] / customer["seats"]
results.append({"name": customer["name"], "arpu": arpu})What does enumerate give me that a plain for customer in customers doesn't?
enumerate pairs each item with its index: (0, customer_0), (1, customer_1). If you only need the item, skip it — for customer in customers is cleaner. enumerate is for when you need "which row am I on?" Here's the full function:
def compute_arpu_per_customer(customers: list) -> list:
results = []
for i, customer in enumerate(customers):
arpu = customer.get("mrr", 0) / max(customer.get("seats", 1), 1)
results.append({"name": customer.get("name", f"customer_{i}"), "arpu": round(arpu, 2)})
print(f"Computed ARPU for {len(results)} customers")
return resultsSo every customer in my export gets an ARPU value in one pass. No dragging formulas, no saving a new CSV column.
And it composes. The output list flows directly into categorize_customer from Day 7, or into the group_by_plan function you'll write next.
I just replaced an Excel column formula with a reusable loop. Composing these functions is the whole game.
max(seats, 1) prevents zero-division. Always guard your denominator — a customer with zero seats would crash mrr / seats without it.
A for loop processes each item in a sequence.
for item in items: # plain iteration
...
for i, item in enumerate(items): # with index
...| Pattern | Use case |
|---|---|
for x in items | Process each item |
for i, x in enumerate(items) | Need position index |
range(n) | Fixed N iterations |
results.append(x) | Build output list |
Division by zero: mrr / seats crashes when seats == 0. Use max(seats, 1) or an if check.
You can filter active customers now with filter_active_customers. But how do you compute ARPU for each one — MRR divided by seats?
In Excel I'd add a column with a formula: =B2/C2. But that's one cell at a time. For 500 rows I'd drag the formula down — except Python doesn't have a spreadsheet UI.
Python has something better. A for loop processes every row in milliseconds, and enumerate() gives you the position index if you need it. You build a new dict per customer with the computed ARPU, and append it to a results list:
for i, customer in enumerate(customers):
arpu = customer["mrr"] / customer["seats"]
results.append({"name": customer["name"], "arpu": arpu})What does enumerate give me that a plain for customer in customers doesn't?
enumerate pairs each item with its index: (0, customer_0), (1, customer_1). If you only need the item, skip it — for customer in customers is cleaner. enumerate is for when you need "which row am I on?" Here's the full function:
def compute_arpu_per_customer(customers: list) -> list:
results = []
for i, customer in enumerate(customers):
arpu = customer.get("mrr", 0) / max(customer.get("seats", 1), 1)
results.append({"name": customer.get("name", f"customer_{i}"), "arpu": round(arpu, 2)})
print(f"Computed ARPU for {len(results)} customers")
return resultsSo every customer in my export gets an ARPU value in one pass. No dragging formulas, no saving a new CSV column.
And it composes. The output list flows directly into categorize_customer from Day 7, or into the group_by_plan function you'll write next.
I just replaced an Excel column formula with a reusable loop. Composing these functions is the whole game.
max(seats, 1) prevents zero-division. Always guard your denominator — a customer with zero seats would crash mrr / seats without it.
A for loop processes each item in a sequence.
for item in items: # plain iteration
...
for i, item in enumerate(items): # with index
...| Pattern | Use case |
|---|---|
for x in items | Process each item |
for i, x in enumerate(items) | Need position index |
range(n) | Fixed N iterations |
results.append(x) | Build output list |
Division by zero: mrr / seats crashes when seats == 0. Use max(seats, 1) or an if check.
You need ARPU (MRR divided by seats) computed for every active customer in your Stripe export — 200 customers, currently done by dragging an Excel formula. Write `compute_arpu_per_customer(customers)` that iterates the list, computes arpu = mrr / seats for each, and returns a list of dicts with 'name' and 'arpu' keys — for example, a customer with mrr=150.0 and seats=3 should produce arpu=50.0.
Tap each step for scaffolded hints.
No blank-editor panic.