Back to Practice
#0130
Count Records by Two Fields
MediumPython25 min20 XP
Problem
Given records and two field names, return a dictionary mapping (field1_value, field2_value) tuples to counts.
Why This Matters
Multi-key grouping is used in dashboards, cohort tables, and feature crosses.
Function Signature
def count_by_pair(records, first, second):
Examples
Example 1
Input[{"city":"NY","plan":"pro"}, {"city":"NY","plan":"pro"}, {"city":"LA","plan":"free"}]
Output{("NY","pro"): 2, ("LA","free"): 1}
The tuple combines both grouping fields.
Constraints
- Return the exact requested structure.
- Handle normal edge cases cleanly.
- Prefer readable code over clever shortcuts.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Input[{"city":"NY","plan":"pro"}, {"city":"NY","plan":"pro"}, {"city":"LA","plan":"free"}]
Expected{("NY","pro"): 2, ("LA","free"): 1}
The tuple combines both grouping fields.
Hidden Test Categories
empty recordsall unique pairssame first field different second field