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.

naive.py
Replay: real traced execution (multi-file project)
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)
  1. 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]values
  2. counts ← {}

    1values = [3, 7, 3, 5, 3, 7, 5, 3]2counts = {}3for v in values:
    values this step{}counts
  3. v ← 3

    2counts = {}3for v in values:4    counts[v] = counts.get(v, 0) + 1
    values this step3v
  4. counts ← {3: 1}

    3for v in values:4    counts[v] = counts.get(v, 0) + 15mode = max(counts, key=counts.get)
    values this step{} {3: 1}counts
  5. v ← 7

    2counts = {}3for v in values:4    counts[v] = counts.get(v, 0) + 1
    values this step3 7v
  6. counts ← {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}counts
  7. v ← 3

    2counts = {}3for v in values:4    counts[v] = counts.get(v, 0) + 1
    values this step7 3v
  8. counts ← {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}counts
  9. v ← 5

    2counts = {}3for v in values:4    counts[v] = counts.get(v, 0) + 1
    values this step3 5v
  10. counts ← {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}counts
  11. v ← 3

    2counts = {}3for v in values:4    counts[v] = counts.get(v, 0) + 1
    values this step5 3v
  12. counts ← {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}counts
  13. v ← 7

    2counts = {}3for v in values:4    counts[v] = counts.get(v, 0) + 1
    values this step3 7v
  14. counts ← {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}counts
  15. v ← 5

    2counts = {}3for v in values:4    counts[v] = counts.get(v, 0) + 1
    values this step7 5v
  16. counts ← {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}counts
  17. v ← 3

    2counts = {}3for v in values:4    counts[v] = counts.get(v, 0) + 1
    values this step5 3v
  18. counts ← {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}counts
  19. for v in values:

    2counts = {}3for v in values:4    counts[v] = counts.get(v, 0) + 1
  20. mode ← 3

    4    counts[v] = counts.get(v, 0) + 15mode = max(counts, key=counts.get)6print('RESULT:', mode)
    values this step3mode
  21. stdout ← 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.

library.py
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.mode for 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.