Skip to Content
Course content

177: The itertools Module: groupby

Click on the "Edit" button in the top corner of the screen to edit your slide content.

How does groupby actually work?

Think of itertools.groupby as a tool that scans through your data and says, "Okay, I'm seeing a bunch of the same things here; I'll bundle them together until I hit something different." It doesn't look ahead or search the whole list—it just looks at the current streak of identical values.

It returns an iterator that yields pairs: a key (the value being grouped) and a group (another iterator containing all the items in that streak). I usually convert that inner group to a list immediately because if you don't, you'll lose the data the moment the main loop moves to the next key.

from itertools import groupby

# A simple list of order statuses
orders = ['Pending', 'Pending', 'Shipped', 'Shipped', 'Shipped', 'Delivered']

for status, group in groupby(orders):
    print(f"Status: {status}, Count: {len(list(group))}")

# Output:
# Status: Pending, Count: 2
# Status: Shipped, Count: 3
# Status: Delivered, Count: 1

Wait, why is it missing some of my groups?

This is the single most common mistake I see with groupby. If your data isn't sorted by the grouping key, groupby will create a new group every time the value changes. It's not like a SQL GROUP BY statement that scans the whole table; it's a linear scan.

If you have a list like ['A', 'B', 'A'], groupby will give you three groups: one for 'A', one for 'B', and another one for 'A'. If you want all the 'A's together, you must sort the data first. It feels redundant, but it's how the tool is designed to stay memory-efficient.

from itertools import groupby

data = ['Apple', 'Banana', 'Apple', 'Cherry', 'Banana']

# WRONG WAY:
for key, group in groupby(data):
    print(key, list(group)) 
# This gives us 5 groups because nothing is consecutive.

# RIGHT WAY:
data.sort() 
for key, group in groupby(data):
    print(key, list(group))
# Now we get Apple [Apple, Apple], Banana [Banana, Banana], etc.

Can I group by something other than the item itself?

Absolutely. You can pass a key function as the second argument. This is incredibly useful when you're dealing with complex objects, like dictionaries or class instances, and you only want to group by one specific property.

For example, imagine you have a list of employees and you want to group them by their department. You'd use a lambda function or operator.itemgetter to tell groupby which field to look at. Just remember: you have to sort the list by that same key function before grouping, or you'll run into the same issue we just discussed.

from itertools import groupby
from operator import itemgetter

employees = [
    {'name': 'Alice', 'dept': 'Engineering'},
    {'name': 'Bob', 'dept': 'Sales'},
    {'name': 'Charlie', 'dept': 'Engineering'},
    {'name': 'David', 'dept': 'Sales'},
]

# Sort by 'dept' first!
employees.sort(key=itemgetter('dept'))

for dept, group in groupby(employees, key=itemgetter('dept')):
    print(f"Department: {dept}")
    for emp in group:
        print(f"  - {emp['name']}")

# Output:
# Department: Engineering
#   - Alice
#   - Charlie
# Department: Sales
#   - Bob
#   - David



📋 Practical Task

Exercise: Organizing Transaction Logs by Date

You have a list of transaction tuples in the format (date, amount, category). The list is currently unsorted. Your task is to use itertools.groupby to calculate the total spend for each unique date.

Requirements:

  • Sort the data by date first.
  • Use groupby to isolate transactions for each date.
  • Sum the amount for all transactions within that date group.
  • Print the result as: Date: YYYY-MM-DD, Total: X
transactions = [
    ('2023-10-01', 50.00, 'Groceries'),
    ('2023-10-02', 20.00, 'Coffee'),
    ('2023-10-01', 15.50, 'Transport'),
    ('2023-10-03', 100.00, 'Electronics'),
    ('2023-10-02', 35.00, 'Lunch'),
]
# Your code here
Rating
0 0

There are no comments for now.

to be the first to leave a comment.