Last week you formatted one survey response — one field, one value, one category. Now your advisor wants a cross-tab of all 500 responses grouped by year-in-school. How do you do that in Excel?
Pivot table. Which I'll remake every time there's new data. And if the columns change, the pivot breaks.
In Python, you don't remake anything. A list holds all 500 responses the way a column holds 500 rows — except you can filter it, loop over every item, and group it by any field without touching a cell. One variable, one collection, one loop. No pivot required.
How do you get from a list of 500 response dicts to a grouped summary by year-in-school?
That's the arc of the week. You start by filtering the list to responses that pass is_valid_response. Then you loop over each one to extract the satisfaction score. A while loop finds the first low-scoring response without scanning the entire dataset. A dict groups responses by demographic field the way a pivot groups by category. And the final lesson nests those structures so each group carries its own count and average. By the end, your cross-tab is a function call.
filter_complete_responses: filter a list of response dicts using is_valid_responsescore_per_response: for loop over the list, extract satisfaction score per rowfind_first_low_score: while loop + break — stop at the first under-threshold responsegroup_by_demographic: dict — accumulate responses into demographic bucketsdemographic_summary: nested structures — count and avg satisfaction per groupGoal: replace a manual pivot table with a function that groups 500 responses by any demographic field.
7 lessons this week
Last week you formatted one survey response — one field, one value, one category. Now your advisor wants a cross-tab of all 500 responses grouped by year-in-school. How do you do that in Excel?
Pivot table. Which I'll remake every time there's new data. And if the columns change, the pivot breaks.
In Python, you don't remake anything. A list holds all 500 responses the way a column holds 500 rows — except you can filter it, loop over every item, and group it by any field without touching a cell. One variable, one collection, one loop. No pivot required.
How do you get from a list of 500 response dicts to a grouped summary by year-in-school?
That's the arc of the week. You start by filtering the list to responses that pass is_valid_response. Then you loop over each one to extract the satisfaction score. A while loop finds the first low-scoring response without scanning the entire dataset. A dict groups responses by demographic field the way a pivot groups by category. And the final lesson nests those structures so each group carries its own count and average. By the end, your cross-tab is a function call.
filter_complete_responses: filter a list of response dicts using is_valid_responsescore_per_response: for loop over the list, extract satisfaction score per rowfind_first_low_score: while loop + break — stop at the first under-threshold responsegroup_by_demographic: dict — accumulate responses into demographic bucketsdemographic_summary: nested structures — count and avg satisfaction per groupGoal: replace a manual pivot table with a function that groups 500 responses by any demographic field.