Here is what I would do
from collections import defaultdict data = ( ('customer1', 'milk', 3), ('customer1', 'bread', 5), ('customer1', 'eggs', 2), ('customer2', 'cheese', 2), ('customer2', 'cereal', 7), ) result = defaultdict(list) for name, what, amount in data: result[name].append((what, amount)) from pprint import pprint result = dict(result) pprint(result)
What seal
{'customer1': [('milk', 3), ('bread', 5), ('eggs', 2)], 'customer2': [('cheese', 2), ('cereal', 7)]}
Nick Craig-Wood
source share