Grouping
Group Mean
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.meanreturnsint(notfloat) when the mean divides evenly for an all-integer list; wrapping withfloat()normalises tofloatso the result type is consistent with the naive version.- For the same aggregation as a pandas one-liner, see the
python-pandastrack lessongroupby-mean, which usesdf.groupby('cat')['val'].mean()— mechanism here, API there. meansis built via{k: ... for k in sorted(totals)}so keys appear in alphabetical order regardless of insertion order.