Single key grouping
Input
group_counts([{'city': 'NY'}, {'city': 'SF'}, {'city': 'NY'}], 'city')
Output
{'NY': 2, 'SF': 1}
Counts occurrences by the 'city' value.
Full lesson preview
Group a list of record dictionaries by one or more keys and compute counts for each grouping.
Problem statement
Task
Examples
Input
group_counts([{'city': 'NY'}, {'city': 'SF'}, {'city': 'NY'}], 'city')
Output
{'NY': 2, 'SF': 1}
Counts occurrences by the 'city' value.
Input format
Output format
Constraints
Samples
Input
group_counts([{'city': 'NY', 'dept': 'sales'}, {'city':'NY','dept':'sales'}, {'city':'SF','dept':'sales'}], ['city','dept'])
Output
{'NY': {'sales': 2}, 'SF': {'sales': 1}}
First group by city, then within each city count by department.