Word frequency counter using reduce() Dry Run in PYTHON

Word frequency counter using reduce() is an interactive PYTHON dry run visualizer from the Special programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Word frequency counter using reduce() Program Code

from functools import reduce

text = "the quick brown fox jumps over the lazy dog and the dog runs quick"
words = text.lower().split()

# Count frequency using reduce
word_count = reduce(
    lambda freq, word: {**freq, word: freq.get(word, 0) + 1},
    words,
    {}
)

# Find most frequent word using reduce
most_frequent = reduce(
    lambda a, b: a if a[1] > b[1] else b,
    word_count.items()
)

print("Word Frequency:")
for word, count in sorted(word_count.items(), key=lambda x: x[1], reverse=True):
    print(f"  {word}: {count}")

print(f"\nMost frequent word: '{most_frequent[0]}' ({most_frequent[1]} times)")

View the complete Special programs page.

Program Console Word frequency counter using reduce() Topic: PYTHON Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.