Using is_over_hours from Week 1 — that comparison applied to one client at a time. What about your six-client list? How do you find only the ones with meaningful hours this month?
I scroll through the spreadsheet and manually highlight the active ones. Usually that means anyone over two hours.
A Python list holds all six client dicts. Index with [0], slice with [1:3], check membership with in. To filter: build an empty list, loop, append what passes the test. The pattern works for two clients or two hundred:
clients = [
{"name": "Acme", "rate": 120.0, "hours": 4.5},
{"name": "Ghost", "rate": 80.0, "hours": 0.0}
]
active = []
for c in clients:
if c["hours"] > 2.0:
active.append(c)Why append to a new list instead of modifying the original? I thought you could just delete the rows you don't want.
You could modify in place, but filtering into a new list keeps the original intact for other functions. You might want the full list for a different report. Non-destructive patterns save debugging time.
So I pass the filtered list to compute_totals_per_client next. The data flows from function to function.
That is the pipeline forming. Filter, then compute, then group. Six clients, one month, zero manual highlighting:
def filter_active_clients(clients: list, min_hours: float) -> list:
active = []
for c in clients:
if c.get("hours", 0) > min_hours:
active.append(c)
print(f"Active clients: {len(active)}")
return activeI just replaced my monthly highlight-and-count with four lines.
One edge case: if a client dict is missing the hours key, c["hours"] raises KeyError. .get("hours", 0) returns zero safely — the client is filtered out rather than crashing your script.
A list comprehension with a condition is the cleanest way to filter client records:
active = [c for c in clients if c['hours'] > min_hours]The equivalent loop version:
active = []
for c in clients:
if c['hours'] > min_hours:
active.append(c)Both produce the same result. The comprehension is more concise; the loop is easier to extend when you need to add logging or handle edge cases.
Dict indexing: c['hours'] reads the hours key. If the key is missing, you get a KeyError — use c.get('hours', 0) to default to zero safely.
Using is_over_hours from Week 1 — that comparison applied to one client at a time. What about your six-client list? How do you find only the ones with meaningful hours this month?
I scroll through the spreadsheet and manually highlight the active ones. Usually that means anyone over two hours.
A Python list holds all six client dicts. Index with [0], slice with [1:3], check membership with in. To filter: build an empty list, loop, append what passes the test. The pattern works for two clients or two hundred:
clients = [
{"name": "Acme", "rate": 120.0, "hours": 4.5},
{"name": "Ghost", "rate": 80.0, "hours": 0.0}
]
active = []
for c in clients:
if c["hours"] > 2.0:
active.append(c)Why append to a new list instead of modifying the original? I thought you could just delete the rows you don't want.
You could modify in place, but filtering into a new list keeps the original intact for other functions. You might want the full list for a different report. Non-destructive patterns save debugging time.
So I pass the filtered list to compute_totals_per_client next. The data flows from function to function.
That is the pipeline forming. Filter, then compute, then group. Six clients, one month, zero manual highlighting:
def filter_active_clients(clients: list, min_hours: float) -> list:
active = []
for c in clients:
if c.get("hours", 0) > min_hours:
active.append(c)
print(f"Active clients: {len(active)}")
return activeI just replaced my monthly highlight-and-count with four lines.
One edge case: if a client dict is missing the hours key, c["hours"] raises KeyError. .get("hours", 0) returns zero safely — the client is filtered out rather than crashing your script.
A list comprehension with a condition is the cleanest way to filter client records:
active = [c for c in clients if c['hours'] > min_hours]The equivalent loop version:
active = []
for c in clients:
if c['hours'] > min_hours:
active.append(c)Both produce the same result. The comprehension is more concise; the loop is easier to extend when you need to add logging or handle edge cases.
Dict indexing: c['hours'] reads the hours key. If the key is missing, you get a KeyError — use c.get('hours', 0) to default to zero safely.
Kai has six active clients but only wants to compute invoices for the ones who logged meaningful hours this month. Write `filter_active_clients(clients, min_hours)` that returns only the client dicts where `hours` exceeds `min_hours`. Each client dict has at least `name`, `rate`, and `hours` keys.
Tap each step for scaffolded hints.
No blank-editor panic.