#231 · Working with data · Beginner · 30 XP · about 6 min
A help desk exports its support tickets as a list of dictionaries, and most tickets have a 'category' such as 'billing' or 'login'. Write count_by_category(tickets) that counts the tickets in each category and returns a list of (category, count) tuples: the most tickets first, and categories with the same count in alphabetical order.
Some tickets were filed in a hurry. A ticket with no 'category' key, or whose category is the empty string '', counts under 'uncategorised'.
tickets = [ {'id': 1, 'category': 'billing'}, {'id': 2, 'category': 'login'}, {'id': 3, 'category': 'billing'}, {'id': 4}, {'id': 5, 'category': 'login'}, {'id': 6, 'category': 'billing'}, ] count_by_category(tickets) # returns [('billing', 3), ('login', 2), ('uncategorised', 1)] count_by_category([{'id': 7, 'category': 'shipping'}, {'id': 8, 'category': 'refunds'}, {'id': 9, 'category': ''}]) # returns [('refunds', 1), ('shipping', 1), ('uncategorised', 1)]
0 <= len(tickets) <= 10000First solves only, ranked by stars and then time.