Python Tutorial / Aggregate and Combine Iteration with collections and itertools
collections provides specialized containers for counting and grouping, while itertools combines iteration flows. Choose them according to ordering, consumption and default-value behavior rather than code length alone.
Count occurrences or accumulate values
Counter counts occurrences of items. defaultdict creates a default through its factory when a missing key is accessed with brackets, making accumulation convenient. That key-creation behavior differs from a simple dictionary get call.
If merely reading a missing key should not insert it, an ordinary dictionary with get may communicate the rule more clearly. These tools express iteration intent rather than eliminating the need to understand what is processed.
For the relevant APIs and platform requirements, see Aggregate and Combine Iteration with collections and itertools documentation.
Run counts, totals and grouped iteration
Save the complete code below as collections_demo.py in a separate practice folder, using UTF-8. Run python collections_demo.py in a terminal with your project virtual environment selected. Outside a virtual environment on macOS or Ubuntu, use python3 when that is your interpreter command.
from collections import Counter, defaultdict
from itertools import chain, groupby
rows = [("food", 3500), ("bus", 2500), ("food", 8000)]
counts = Counter(category for category, _ in rows)
totals = defaultdict(int)
for category, amount in rows:
totals[category] += amount
print(sorted(counts.items()))
print(sorted(totals.items()))
print(list(chain([1, 2], [3])))
for category, group in groupby(sorted(rows), key=lambda row: row[0]):
print(category, sum(amount for _, amount in group))
Expected output:
[('bus', 1), ('food', 2)]
[('bus', 2500), ('food', 11500)]
[1, 2, 3]
bus 2500
food 11500
Remember that groupby groups adjacent keys
itertools.groupby forms a group while equal keys remain consecutive. It does not automatically collect all equal keys scattered through the input like SQL GROUP BY. Sorting by the intended key first brings the matching records together in this example.
Group iterators share the underlying iteration stream. After advancing to the next group, do not expect to read the previous group again. Consume it immediately or retain the needed list or aggregate before moving on.
Consider memory and the meaning of the result
chain reads collections in sequence; islice can select part of an iterator. Converting the final result to a list stores the entire result again. Infinite iterators require a limit such as islice or an explicit termination condition.
Remove sorted from the groupby input and observe food as two separate groups, then restore it. Use Counter for occurrences and an accumulation rule for amounts: counting two food records is different from summing their costs. The generator guide explains the underlying consumption model.




