treatment_summary from Day 14 returns a dict — group names to stats. Your methods table needs those groups ranked by mean outcome, highest first. In SPSS, that's a sort operation on a pivot table. What's the Python equivalent?
extract_respondent_ids from Day 25 showed sorted with a lambda key. I could apply the same pattern to the groups dict — sort by the mean_outcome value inside each group's stats dict.
Exactly. sorted on groups.items() gives you (group_name, stats_dict) pairs you can sort by the mean_outcome field inside the stats dict. One lambda, one sort:
groups = group_by_treatment(respondents)
ranked = sorted(
groups.items(),
key=lambda x: x[1]["mean_outcome"],
reverse=True
)Why x[1]["mean_outcome"]? What is x in this lambda?
.items() returns (key, value) pairs — so each x is a two-element tuple: x[0] is the group name, x[1] is the stats dict. x[1]["mean_outcome"] navigates: pair → stats dict → mean outcome value. Then sorted orders those pairs by that value.
So the result is a list of (group_name, stats_dict) tuples, sorted by mean outcome. I can format that directly into the ranked methods table.
The entire SPSS sort-and-pivot, in four lines:
def rank_groups_by_outcome(respondents: list) -> list:
groups = group_by_treatment(respondents)
summary = treatment_summary(respondents)
ranked = sorted(summary.items(), key=lambda x: x[1]["mean_outcome"], reverse=True)
result = [{"group": g, "mean_outcome": s["mean_outcome"], "n": s["n"]} for g, s in ranked]
print(f"Ranked {len(result)} groups")
return resultrank_groups_by_outcome + make_table_row from Day 17 and the formatted ranked table builds itself. That's the capstone pipeline in two function calls.
The ranked list output is a list of dicts — serialisable to JSON, passable to formatting functions, and reproducible. Avoid returning the raw .items() tuple — it's less readable and harder to serialise. Convert to named dicts before returning.
sorted(iterable, key=func, reverse=True) returns a new sorted list. The key function is called on each item to produce the sort value.
sorted(summary.items(), key=lambda x: x[1]["mean_outcome"], reverse=True)lambda x: x[1]["mean_outcome"] — x is a (group, stats) tuple; x[1] is the stats dict; ["mean_outcome"] gets the value.
sorted(lst) returns a new list — the original is unchanged. lst.sort() modifies in place and returns None. For a function that takes input and returns a result, always use sorted.
treatment_summary from Day 14 returns a dict — group names to stats. Your methods table needs those groups ranked by mean outcome, highest first. In SPSS, that's a sort operation on a pivot table. What's the Python equivalent?
extract_respondent_ids from Day 25 showed sorted with a lambda key. I could apply the same pattern to the groups dict — sort by the mean_outcome value inside each group's stats dict.
Exactly. sorted on groups.items() gives you (group_name, stats_dict) pairs you can sort by the mean_outcome field inside the stats dict. One lambda, one sort:
groups = group_by_treatment(respondents)
ranked = sorted(
groups.items(),
key=lambda x: x[1]["mean_outcome"],
reverse=True
)Why x[1]["mean_outcome"]? What is x in this lambda?
.items() returns (key, value) pairs — so each x is a two-element tuple: x[0] is the group name, x[1] is the stats dict. x[1]["mean_outcome"] navigates: pair → stats dict → mean outcome value. Then sorted orders those pairs by that value.
So the result is a list of (group_name, stats_dict) tuples, sorted by mean outcome. I can format that directly into the ranked methods table.
The entire SPSS sort-and-pivot, in four lines:
def rank_groups_by_outcome(respondents: list) -> list:
groups = group_by_treatment(respondents)
summary = treatment_summary(respondents)
ranked = sorted(summary.items(), key=lambda x: x[1]["mean_outcome"], reverse=True)
result = [{"group": g, "mean_outcome": s["mean_outcome"], "n": s["n"]} for g, s in ranked]
print(f"Ranked {len(result)} groups")
return resultrank_groups_by_outcome + make_table_row from Day 17 and the formatted ranked table builds itself. That's the capstone pipeline in two function calls.
The ranked list output is a list of dicts — serialisable to JSON, passable to formatting functions, and reproducible. Avoid returning the raw .items() tuple — it's less readable and harder to serialise. Convert to named dicts before returning.
sorted(iterable, key=func, reverse=True) returns a new sorted list. The key function is called on each item to produce the sort value.
sorted(summary.items(), key=lambda x: x[1]["mean_outcome"], reverse=True)lambda x: x[1]["mean_outcome"] — x is a (group, stats) tuple; x[1] is the stats dict; ["mean_outcome"] gets the value.
sorted(lst) returns a new list — the original is unchanged. lst.sort() modifies in place and returns None. For a function that takes input and returns a result, always use sorted.
Ian needs to rank treatment groups by mean outcome for a methods table. Write `rank_groups_by_outcome(respondents)` that calls `group_by_treatment` and `treatment_summary` internally, then sorts groups by mean outcome descending and returns a list of `{"group": ..., "mean_outcome": ..., "n": ...}` dicts.
Tap each step for scaffolded hints.
No blank-editor panic.