You have group_by_treatment from Day 13 returning a dict of respondent lists per group. Now the methods section needs N, mean outcome, and mean age for each group. Where do those numbers come from?
group_by_treatment gives {"control": [r1, r2, ...]}. For each group, N is len(group_list), mean outcome is sum(outcomes) / len(outcomes). I need to loop over the groups dict.
Exactly — .items() gives (group_name, respondent_list) pairs, so you can compute stats for each group in one loop. The result is a nested dict: a dict of dicts, where each outer key is the group and each inner dict holds the stats:
for group, members in groups.items():
outcomes = [r["outcome"] for r in members]
ages = [r["age"] for r in members]
n = len(members)
mean_outcome = sum(outcomes) / n
mean_age = sum(ages) / nWhat's [r["outcome"] for r in members]? Is that a loop inside brackets?
A list comprehension — a compact way to build a list by transforming each item. [r["outcome"] for r in members] is the same as a for loop that appends r["outcome"] to a result list. You'll build these from scratch in Week 4; for now read it as "the outcome from each respondent in the group".
So the output is {"control": {"n": 87, "mean_outcome": 4.72, "mean_age": 34.2}} — the entire methods table, as a Python dict.
Paste that into json.dumps from Week 3 and it's supplementary materials ready for submission:
def treatment_summary(respondents: list) -> dict:
groups = group_by_treatment(respondents)
summary = {}
for group, members in groups.items():
outcomes = [r["outcome"] for r in members]
ages = [r["age"] for r in members]
n = len(members)
summary[group] = {"n": n, "mean_outcome": round(sum(outcomes)/n, 2), "mean_age": round(sum(ages)/n, 2)}
print(f"Summary groups: {list(summary.keys())}")
return summaryWeek 2 is the entire descriptive statistics section of my methods. In code.
One edge case: if a group has zero members after filtering, len(members) is 0 and the division crashes with ZeroDivisionError. Guard with if members: before computing stats, or use filter_eligible upstream to ensure every group has at least one respondent before summary runs.
A dict of dicts is the natural shape of a summary table — outer key is the group, inner dict holds the stats.
summary = {
"control": {"n": 87, "mean_outcome": 4.72, "mean_age": 34.2},
"treatment_a": {"n": 93, "mean_outcome": 5.01, "mean_age": 33.8}
}for key, value in d.items():
...Returns (key, value) pairs — the cleanest way to iterate a dict when you need both the key and the value.
if members:
mean = sum(values) / len(members)
else:
mean = 0.0You have group_by_treatment from Day 13 returning a dict of respondent lists per group. Now the methods section needs N, mean outcome, and mean age for each group. Where do those numbers come from?
group_by_treatment gives {"control": [r1, r2, ...]}. For each group, N is len(group_list), mean outcome is sum(outcomes) / len(outcomes). I need to loop over the groups dict.
Exactly — .items() gives (group_name, respondent_list) pairs, so you can compute stats for each group in one loop. The result is a nested dict: a dict of dicts, where each outer key is the group and each inner dict holds the stats:
for group, members in groups.items():
outcomes = [r["outcome"] for r in members]
ages = [r["age"] for r in members]
n = len(members)
mean_outcome = sum(outcomes) / n
mean_age = sum(ages) / nWhat's [r["outcome"] for r in members]? Is that a loop inside brackets?
A list comprehension — a compact way to build a list by transforming each item. [r["outcome"] for r in members] is the same as a for loop that appends r["outcome"] to a result list. You'll build these from scratch in Week 4; for now read it as "the outcome from each respondent in the group".
So the output is {"control": {"n": 87, "mean_outcome": 4.72, "mean_age": 34.2}} — the entire methods table, as a Python dict.
Paste that into json.dumps from Week 3 and it's supplementary materials ready for submission:
def treatment_summary(respondents: list) -> dict:
groups = group_by_treatment(respondents)
summary = {}
for group, members in groups.items():
outcomes = [r["outcome"] for r in members]
ages = [r["age"] for r in members]
n = len(members)
summary[group] = {"n": n, "mean_outcome": round(sum(outcomes)/n, 2), "mean_age": round(sum(ages)/n, 2)}
print(f"Summary groups: {list(summary.keys())}")
return summaryWeek 2 is the entire descriptive statistics section of my methods. In code.
One edge case: if a group has zero members after filtering, len(members) is 0 and the division crashes with ZeroDivisionError. Guard with if members: before computing stats, or use filter_eligible upstream to ensure every group has at least one respondent before summary runs.
A dict of dicts is the natural shape of a summary table — outer key is the group, inner dict holds the stats.
summary = {
"control": {"n": 87, "mean_outcome": 4.72, "mean_age": 34.2},
"treatment_a": {"n": 93, "mean_outcome": 5.01, "mean_age": 33.8}
}for key, value in d.items():
...Returns (key, value) pairs — the cleanest way to iterate a dict when you need both the key and the value.
if members:
mean = sum(values) / len(members)
else:
mean = 0.0Hassan has filtered and grouped respondents using `group_by_treatment`. Write `treatment_summary(respondents)` that calls `group_by_treatment` internally, then computes N, mean outcome, and mean age for each group. Return a dict like `{"control": {"n": 2, "mean_outcome": 4.15, "mean_age": 25.5}}`.
Tap each step for scaffolded hints.
No blank-editor panic.