In a grouped total, the grouping field becomes a dictionary key. Each record contributes its own measurement to that key.
Work out how many stops each tour has visited.
visits = [{"tour": "Art"}, {"tour": "Food"}, {"tour": "Art"}]
counts = {}
for visit in visits:
tour = visit["tour"]
counts[tour] = counts.get(tour, 0) + 1
print(counts) # {'Art': 2, 'Food': 1}Only the current tour changes on each pass. To total a measurement, add the amount in the record instead of counting the record itself.
Complete line_distances(trips). Return total kilometers per line from records with line and km. Include a line even if its only trip has zero kilometers.
trips = [{"line": "A", "km": 3}, {"line": "A", "km": 4}]
line_distances(trips) # {"A": 7}