Central Tendency
Mode Frequency
Find the mode — the most frequently occurring value in a dataset. Tally
how many times each value appears, then pick the value with the highest
count. By hand, build a frequency dict and use max. With the library,
statistics.mode does both steps in one call.
By hand
Build counts by iterating the values and incrementing each key with
counts.get(v, 0) + 1. The trace shows the dict filling in: 3 is
incremented on iterations 0, 2, 4, 7 and ends at 4 — the highest count.
max(counts, key=counts.get) picks the key whose mapped count is largest.
values = [3, 7, 3, 5, 3, 7, 5, 3]
counts = {}
for v in values:
counts[v] = counts.get(v, 0) + 1
mode = max(counts, key=counts.get)
print('RESULT:', mode)
values ← [3, 7, 3, 5, 3, 7, 5, 3]
1values = [3, 7, 3, 5, 3, 7, 5, 3]2counts = {}values this step[3, 7, 3, 5, 3, 7, 5, 3]valuescounts ← {}
1values = [3, 7, 3, 5, 3, 7, 5, 3]2counts = {}3for v in values:values this step{}countsv ← 3
2counts = {}3for v in values:4 counts[v] = counts.get(v, 0) + 1values this step3vcounts ← {3: 1}
3for v in values:4 counts[v] = counts.get(v, 0) + 15mode = max(counts, key=counts.get)values this step{} → {3: 1}countsv ← 7
2counts = {}3for v in values:4 counts[v] = counts.get(v, 0) + 1values this step3 → 7vcounts ← {3: 1, 7: 1}
3for v in values:4 counts[v] = counts.get(v, 0) + 15mode = max(counts, key=counts.get)values this step{3: 1} → {3: 1, 7: 1}countsv ← 3
2counts = {}3for v in values:4 counts[v] = counts.get(v, 0) + 1values this step7 → 3vcounts ← {3: 2, 7: 1}
3for v in values:4 counts[v] = counts.get(v, 0) + 15mode = max(counts, key=counts.get)values this step{3: 1, 7: 1} → {3: 2, 7: 1}countsv ← 5
2counts = {}3for v in values:4 counts[v] = counts.get(v, 0) + 1values this step3 → 5vcounts ← {3: 2, 7: 1, 5: 1}
3for v in values:4 counts[v] = counts.get(v, 0) + 15mode = max(counts, key=counts.get)values this step{3: 2, 7: 1} → {3: 2, 7: 1, 5: 1}countsv ← 3
2counts = {}3for v in values:4 counts[v] = counts.get(v, 0) + 1values this step5 → 3vcounts ← {3: 3, 7: 1, 5: 1}
3for v in values:4 counts[v] = counts.get(v, 0) + 15mode = max(counts, key=counts.get)values this step{3: 2, 7: 1, 5: 1} → {3: 3, 7: 1, 5: 1}countsv ← 7
2counts = {}3for v in values:4 counts[v] = counts.get(v, 0) + 1values this step3 → 7vcounts ← {3: 3, 7: 2, 5: 1}
3for v in values:4 counts[v] = counts.get(v, 0) + 15mode = max(counts, key=counts.get)values this step{3: 3, 7: 1, 5: 1} → {3: 3, 7: 2, 5: 1}countsv ← 5
2counts = {}3for v in values:4 counts[v] = counts.get(v, 0) + 1values this step7 → 5vcounts ← {3: 3, 7: 2, 5: 2}
3for v in values:4 counts[v] = counts.get(v, 0) + 15mode = max(counts, key=counts.get)values this step{3: 3, 7: 2, 5: 1} → {3: 3, 7: 2, 5: 2}countsv ← 3
2counts = {}3for v in values:4 counts[v] = counts.get(v, 0) + 1values this step5 → 3vcounts ← {3: 4, 7: 2, 5: 2}
3for v in values:4 counts[v] = counts.get(v, 0) + 15mode = max(counts, key=counts.get)values this step{3: 3, 7: 2, 5: 2} → {3: 4, 7: 2, 5: 2}countsfor v in values:
2counts = {}3for v in values:4 counts[v] = counts.get(v, 0) + 1mode ← 3
4 counts[v] = counts.get(v, 0) + 15mode = max(counts, key=counts.get)6print('RESULT:', mode)values this step3modestdout ← RESULT: 3
5mode = max(counts, key=counts.get)6print('RESULT:', mode)values this stepRESULT: 3stdout
With the library
statistics.mode returns the single most frequent value. When multiple
values tie for the highest count, Python 3.8+ returns the first one
encountered; older Python raises StatisticsError. The data here has a
unique mode so neither edge case applies.
import statistics
from dalib.display import set_display
set_display()
values = [3, 7, 3, 5, 3, 7, 5, 3]
mode = statistics.mode(values)
print('statistics.mode:', mode)
print('RESULT:', mode)
statistics.mode: 3
RESULT: 3
Implementation notes
max(counts, key=counts.get)returns the key (value) whose count is largest; it matches Python 3.8+statistics.modefor a unique mode.- For multimodal data (multiple equally frequent values), use
statistics.multimode(Python 3.8+) to get all modes as a list. - Cross-reference:
frequency-count(python-data-basics) for the general pattern of tallying element frequencies.