Compute the mean value per category from parallel label and value lists. Each category accumulates a running total and a count; dividing at the end gives the mean. The replay shows totals and counts evolving key by key before the final means dict is assembled in one comprehension.

By hand

The Pythonic way

One pass with defaultdict(list) collects every value into its group list. A dict comprehension over sorted keys calls statistics.mean on each group, with float() to normalise the return type.

naive.py
cats = ['a', 'b', 'a', 'c', 'b', 'a', 'c', 'b']
vals = [2, 9, 4, 6, 3, 6, 10, 6]
totals = {}
counts = {}
for cat, val in zip(cats, vals):
    if cat not in totals:
        totals[cat] = 0
        counts[cat] = 0
    totals[cat] = totals[cat] + val
    counts[cat] = counts[cat] + 1
means = {k: totals[k] / counts[k] for k in sorted(totals)}
print('RESULT:', means)
library.py
import statistics
from collections import defaultdict
cats = ['a', 'b', 'a', 'c', 'b', 'a', 'c', 'b']
vals = [2, 9, 4, 6, 3, 6, 10, 6]
groups = defaultdict(list)
for cat, val in zip(cats, vals):
    groups[cat].append(val)
means = {k: float(statistics.mean(groups[k])) for k in sorted(groups)}
print('RESULT:', means)
RESULT: {'a': 4.0, 'b': 6.0, 'c': 8.0}

Implementation notes

  • statistics.mean returns int (not float) when the mean divides evenly for an all-integer list; wrapping with float() normalises to float so the result type is consistent with the naive version.
  • For the same aggregation as a pandas one-liner, see the python-pandas track lesson groupby-mean, which uses df.groupby('cat')['val'].mean() — mechanism here, API there.
  • means is built via {k: ... for k in sorted(totals)} so keys appear in alphabetical order regardless of insertion order.