← Back to all snippets
PYTHON

Grouping Data by Key Using `itertools.groupby`

Efficiently group adjacent identical elements in a sorted list or iterable using Python's `itertools.groupby` for powerful data aggregation tasks in web applications.

import itertools

class Transaction:
    def __init__(self, user_id, amount):
        self.user_id = user_id
        self.amount = amount

    def __repr__(self):
        return f"Transaction(user_id={self.user_id}, amount={self.amount})"

transactions = [
    Transaction(101, 50.0),
    Transaction(102, 120.0),
    Transaction(101, 75.0),
    Transaction(103, 200.0),
    Transaction(102, 30.0),
    Transaction(101, 10.0),
]

# Step 1: Sort the data by the key you want to group by
# itertools.groupby requires elements to be sorted by the key
transactions.sort(key=lambda t: t.user_id)

# Step 2: Use itertools.groupby
grouped_transactions = {}
for user_id, group in itertools.groupby(transactions, key=lambda t: t.user_id):
    grouped_transactions[user_id] = list(group) # Convert group iterator to a list

# Result would be:
# {
#     101: [Transaction(user_id=101, amount=50.0), Transaction(user_id=101, amount=75.0), Transaction(user_id=101, amount=10.0)],
#     102: [Transaction(user_id=102, amount=120.0), Transaction(user_id=102, amount=30.0)],
#     103: [Transaction(user_id=103, amount=200.0)]
# }

# You could also sum up amounts per user:
user_total_amounts = {
    user_id: sum(t.amount for t in group)
    for user_id, group in itertools.groupby(transactions, key=lambda t: t.user_id)
}
# Result: {101: 135.0, 102: 150.0, 103: 200.0}
How it works: This snippet demonstrates using `itertools.groupby` to group elements in an iterable based on a common key. This is incredibly useful for aggregating data, such as calculating totals per user, categorizing items, or generating reports in web applications. Crucially, `itertools.groupby` only groups *adjacent* identical keys, so the input iterable *must be sorted* by the grouping key beforehand. The example shows how to group `Transaction` objects by `user_id` and then either collect all transactions for each user or efficiently sum their amounts.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs