top_groups_by_score from Day 24 already sorted. But what if you want the full ranked list — every group, not just the top N — with each group's rank, average, and count? Your methods table needs that.
extract_free_text_themes gave me string extraction. But sorting dicts by a nested value — sorted must have a way to pick which field to sort by.
sorted(key=...) is the answer. Pass a lambda function as the key — it takes one item and returns the value to sort by. lambda x: x["avg_satisfaction"] means "sort by the avg_satisfaction field, ascending". Add reverse=True for descending:
groups = [{"group": "Junior", "avg_satisfaction": 3.5}, {"group": "Senior", "avg_satisfaction": 4.2}]
ranked = sorted(groups, key=lambda x: x["avg_satisfaction"], reverse=True)
print(ranked) # Senior first, Junior secondA lambda is just a tiny function I write inline? lambda x: x["avg_satisfaction"] is the same as writing def get_avg(x): return x["avg_satisfaction"]?
Exactly the same thing. A lambda is a one-line anonymous function — you use it when a small function is only needed once, right here, and naming it would be more code than the logic itself:
def rank_groups_by_satisfaction(responses: list, field: str) -> list:
"""Return all demographic groups ranked from highest to lowest avg satisfaction."""
summary = demographic_summary(responses, field)
ranked = sorted(summary.items(), key=lambda x: x[1]["avg_satisfaction"], reverse=True)
result = [{"group": g, "avg_satisfaction": v["avg_satisfaction"], "count": v["count"]} for g, v in ranked]
print(f"Ranked {len(result)} groups")
return resultsummary.items() gives (group_name, stats_dict) pairs — x[1] is the stats dict. That's the key to sort by.
Your pivot table's "sort descending" button just became one argument to one function.
And I called demographic_summary from Day 14 again. Same function, seventh time, different context.
sorted never modifies the original. It returns a new list. If you need the original group order preserved, the source summary dict is untouched. This is the immutability you want in a data pipeline — transform without mutating.
sorted(items, key=lambda x: x["field"], reverse=True)key= takes a function that maps each item to its sort valuereverse=True sorts descending (highest first)lambda x: expression is a one-line anonymous function. Equivalent to:
def fn(x):
return expressionkey=lambda x: x[1]["avg_satisfaction"] — access the second element of a tuple, then a nested dict key.
Python's sort is stable: items with equal keys keep their original order. This matters when you sort by satisfaction and two groups have identical scores.
top_groups_by_score from Day 24 already sorted. But what if you want the full ranked list — every group, not just the top N — with each group's rank, average, and count? Your methods table needs that.
extract_free_text_themes gave me string extraction. But sorting dicts by a nested value — sorted must have a way to pick which field to sort by.
sorted(key=...) is the answer. Pass a lambda function as the key — it takes one item and returns the value to sort by. lambda x: x["avg_satisfaction"] means "sort by the avg_satisfaction field, ascending". Add reverse=True for descending:
groups = [{"group": "Junior", "avg_satisfaction": 3.5}, {"group": "Senior", "avg_satisfaction": 4.2}]
ranked = sorted(groups, key=lambda x: x["avg_satisfaction"], reverse=True)
print(ranked) # Senior first, Junior secondA lambda is just a tiny function I write inline? lambda x: x["avg_satisfaction"] is the same as writing def get_avg(x): return x["avg_satisfaction"]?
Exactly the same thing. A lambda is a one-line anonymous function — you use it when a small function is only needed once, right here, and naming it would be more code than the logic itself:
def rank_groups_by_satisfaction(responses: list, field: str) -> list:
"""Return all demographic groups ranked from highest to lowest avg satisfaction."""
summary = demographic_summary(responses, field)
ranked = sorted(summary.items(), key=lambda x: x[1]["avg_satisfaction"], reverse=True)
result = [{"group": g, "avg_satisfaction": v["avg_satisfaction"], "count": v["count"]} for g, v in ranked]
print(f"Ranked {len(result)} groups")
return resultsummary.items() gives (group_name, stats_dict) pairs — x[1] is the stats dict. That's the key to sort by.
Your pivot table's "sort descending" button just became one argument to one function.
And I called demographic_summary from Day 14 again. Same function, seventh time, different context.
sorted never modifies the original. It returns a new list. If you need the original group order preserved, the source summary dict is untouched. This is the immutability you want in a data pipeline — transform without mutating.
sorted(items, key=lambda x: x["field"], reverse=True)key= takes a function that maps each item to its sort valuereverse=True sorts descending (highest first)lambda x: expression is a one-line anonymous function. Equivalent to:
def fn(x):
return expressionkey=lambda x: x[1]["avg_satisfaction"] — access the second element of a tuple, then a nested dict key.
Python's sort is stable: items with equal keys keep their original order. This matters when you sort by satisfaction and two groups have identical scores.
You are finalising your thesis results section and need all demographic groups listed from highest to lowest average satisfaction — not just the top N, but the full ranked list. Write `rank_groups_by_satisfaction(responses, field)` that calls `group_by_demographic` and `demographic_summary`, then returns all groups sorted by avg_satisfaction descending as a list of dicts.
Tap each step for scaffolded hints.
No blank-editor panic.