filter_high_spend hands you a shorter list — maybe eight campaigns instead of forty. Now the CMO wants CPL on every single one. In Excel, what's your next move?
filter_high_spend gave me the filtered rows — now I'd add a formula column: spend divided by leads, dragged down for each row.
That drag is a for loop. It visits every item in your list and runs the same logic on each one — no row numbers, no column references:
for c in campaigns:
cpl = c["spend"] / c["leads"]
print(cpl)Each pass, c becomes the next campaign dict. Python does the dragging.
What if I need to know which row I'm on — like to label "Campaign 1", "Campaign 2"? Do I have to track a counter variable myself?
Python built that counter in. enumerate() hands you both the position and the item at once. Here's the full function — format_campaign from Day 3 handles the label, and enumerate tracks the rank:
def compute_cpl_per_campaign(campaigns: list) -> list:
result = []
for i, c in enumerate(campaigns):
label = format_campaign(c["name"], c["spend"])
cpl = c["spend"] / c["leads"]
result.append({"name": label, "cpl": round(cpl, 2)})
print(f"Processed {len(result)} campaigns")
return resulti starts at zero — use i + 1 when you need a one-based rank in your output.
So every campaign dict from filter_high_spend flows straight into this loop. The functions chain — filter first, then compute CPL on what survives.
Exactly — except unlike an Excel formula chain, nobody can break it by inserting a column.
And if I don't need the position at all, I just write for c in campaigns and skip enumerate entirely. Less noise.
That's the rule. Reach for enumerate() only when the index actually earns its place in the output. When the position is irrelevant, the simpler loop is correct.
| Syntax | When to use |
|---|---|
for c in campaigns | Item only — position unused |
for i, c in enumerate(campaigns) | Need both index and item |
enumerate() yields (0, item0), (1, item1), … — unpack with two loop variables.
result = []
for c in campaigns:
result.append({"name": c["name"], "cpl": c["spend"] / c["leads"]})Start with an empty list, append inside the loop, return after.
Returning inside the loop exits after one iteration. return belongs after the loop, at the same indent level as result = [].
filter_high_spend hands you a shorter list — maybe eight campaigns instead of forty. Now the CMO wants CPL on every single one. In Excel, what's your next move?
filter_high_spend gave me the filtered rows — now I'd add a formula column: spend divided by leads, dragged down for each row.
That drag is a for loop. It visits every item in your list and runs the same logic on each one — no row numbers, no column references:
for c in campaigns:
cpl = c["spend"] / c["leads"]
print(cpl)Each pass, c becomes the next campaign dict. Python does the dragging.
What if I need to know which row I'm on — like to label "Campaign 1", "Campaign 2"? Do I have to track a counter variable myself?
Python built that counter in. enumerate() hands you both the position and the item at once. Here's the full function — format_campaign from Day 3 handles the label, and enumerate tracks the rank:
def compute_cpl_per_campaign(campaigns: list) -> list:
result = []
for i, c in enumerate(campaigns):
label = format_campaign(c["name"], c["spend"])
cpl = c["spend"] / c["leads"]
result.append({"name": label, "cpl": round(cpl, 2)})
print(f"Processed {len(result)} campaigns")
return resulti starts at zero — use i + 1 when you need a one-based rank in your output.
So every campaign dict from filter_high_spend flows straight into this loop. The functions chain — filter first, then compute CPL on what survives.
Exactly — except unlike an Excel formula chain, nobody can break it by inserting a column.
And if I don't need the position at all, I just write for c in campaigns and skip enumerate entirely. Less noise.
That's the rule. Reach for enumerate() only when the index actually earns its place in the output. When the position is irrelevant, the simpler loop is correct.
| Syntax | When to use |
|---|---|
for c in campaigns | Item only — position unused |
for i, c in enumerate(campaigns) | Need both index and item |
enumerate() yields (0, item0), (1, item1), … — unpack with two loop variables.
result = []
for c in campaigns:
result.append({"name": c["name"], "cpl": c["spend"] / c["leads"]})Start with an empty list, append inside the loop, return after.
Returning inside the loop exits after one iteration. return belongs after the loop, at the same indent level as result = [].
Kenji has a filtered list of campaign dicts — each with `"name"`, `"spend"`, and `"leads"` keys — and needs to compute cost-per-lead for every entry so the weekly report can rank them. Write `compute_cpl_per_campaign(campaigns)` that iterates the list, computes `spend / leads` for each campaign, and returns a new list of dicts with `"name"` and `"cpl"` (rounded to 2 decimal places). For example, `compute_cpl_per_campaign([{"name": "Email", "spend": 1250.0, "leads": 50}])` should return `[{"name": "Email", "cpl": 25.0}]`.
Tap each step for scaffolded hints.
No blank-editor panic.