Grouping
Group Count
Count how many times each category appears in a list of labels. The naive
loop seeds each new key with 0 the first time it is seen, then increments it
on every visit. The trace shows counts growing one key at a time.
By hand
Check whether each label is already a key in counts; if not, seed it with 0.
Either way, increment by 1. The if x not in counts events are zero-delta
in the trace — the actual change happens on the counts[x] = 0 or
counts[x] = counts[x] + 1 lines.
naive.py
Replay: real traced execution (multi-file project)
labels = ['a', 'b', 'a', 'c', 'b', 'a', 'b', 'a']
counts = {}
for x in labels:
if x not in counts:
counts[x] = 0
counts[x] = counts[x] + 1
print('RESULT:', {k: counts[k] for k in sorted(counts)})
labels ← ['a', 'b', 'a', 'c', 'b', 'a', 'b', 'a']
1labels = ['a', 'b', 'a', 'c', 'b', 'a', 'b', 'a']2counts = {}values this step['a', 'b', 'a', 'c', 'b', 'a', 'b', 'a']labelscounts ← {}
1labels = ['a', 'b', 'a', 'c', 'b', 'a', 'b', 'a']2counts = {}3for x in labels:values this step{}countsx ← 'a', counts ← {'a': 1}
pass 1 of 22counts = {}3for x in labels:4 if x not in counts:5 counts[x] = 06 counts[x] = counts[x] + 17print('RESULT:', {k: counts[k] for k in sorted(counts)})values this step'a'x{'a': 0} → {'a': 1}countsx ← 'b', counts ← {'a': 1, 'b': 1}
pass 2 of 22counts = {}3for x in labels:4 if x not in counts:5 counts[x] = 06 counts[x] = counts[x] + 17print('RESULT:', {k: counts[k] for k in sorted(counts)})values this step'a' → 'b'x{'a': 1, 'b': 0} → {'a': 1, 'b': 1}countsx ← 'a'
2counts = {}3for x in labels:4 if x not in counts:values this step'b' → 'a'xif x not in counts:
3for x in labels:4 if x not in counts:5 counts[x] = 0counts ← {'a': 2, 'b': 1}
5 counts[x] = 06 counts[x] = counts[x] + 17print('RESULT:', {k: counts[k] for k in sorted(counts)})values this step{'a': 1, 'b': 1} → {'a': 2, 'b': 1}countsx ← 'c'
2counts = {}3for x in labels:4 if x not in counts:values this step'a' → 'c'xif x not in counts:
3for x in labels:4 if x not in counts:5 counts[x] = 0counts ← {'a': 2, 'b': 1, 'c': 0}
4if x not in counts:5 counts[x] = 06counts[x] = counts[x] + 1values this step{'a': 2, 'b': 1} → {'a': 2, 'b': 1, 'c': 0}countscounts ← {'a': 2, 'b': 1, 'c': 1}, x ← 'b'
pass 1 of 42counts = {}3for x in labels:4 if x not in counts:5 counts[x] = 06 counts[x] = counts[x] + 17print('RESULT:', {k: counts[k] for k in sorted(counts)})values this step{'a': 2, 'b': 1, 'c': 0} → {'a': 2, 'b': 1, 'c': 1}counts'c' → 'b'xAll 4 passes — pass 1 is the card above pass countsx1 {'a': 2, 'b': 1, 'c': 0} → {'a': 2, 'b': 1, 'c': 1} 'c' → 'b' 2 {'a': 2, 'b': 1, 'c': 1} → {'a': 2, 'b': 2, 'c': 1} 'b' → 'a' 3 {'a': 2, 'b': 2, 'c': 1} → {'a': 3, 'b': 2, 'c': 1} 'a' → 'b' 4 {'a': 3, 'b': 2, 'c': 1} → {'a': 3, 'b': 3, 'c': 1} 'b' → 'a' counts ← {'a': 4, 'b': 3, 'c': 1}
5 counts[x] = 06 counts[x] = counts[x] + 17print('RESULT:', {k: counts[k] for k in sorted(counts)})values this step{'a': 3, 'b': 3, 'c': 1} → {'a': 4, 'b': 3, 'c': 1}countsfor x in labels:
2counts = {}3for x in labels:4 if x not in counts:stdout ← RESULT: {'a': 4, 'b': 3, 'c': 1}
6 counts[x] = counts[x] + 17print('RESULT:', {k: counts[k] for k in sorted(counts)})values this stepRESULT: {'a': 4, 'b': 3, 'c': 1}stdout
The Pythonic way
Counter(labels) does the same counting in one call, returning a Counter
subclass of dict. Sorting the keys before printing ensures a deterministic
RESULT regardless of insertion order.
library.py
from collections import Counter
labels = ['a', 'b', 'a', 'c', 'b', 'a', 'b', 'a']
counts = Counter(labels)
print('RESULT:', {k: counts[k] for k in sorted(counts)})
RESULT: {'a': 4, 'b': 3, 'c': 1}
Implementation notes
countsgrows in insertion order (afirst, thenb, thenc); the printed RESULT uses a comprehension oversorted(counts)to give alphabetical order regardless.Counteralso supportscounts.most_common()for frequency-sorted output, and arithmetic between counters.- The
if x not in counts: counts[x] = 0idiom is equivalent tocounts.setdefault(x, 0)orcounts.get(x, 0) + 1.