Reference library

Dictionaries & sets

Key–value maps, uniqueness, counting, grouping, and fast lookups.

1 match
Dictionaries & sets easy

Count Word Frequency in Python with dict

Count how often each word appears in a text using Python's collections.Counter and regular expressions.

dictionary counter frequency
Python
from collections import Counter
import re

def count_word_frequency(text):
    """Count frequency of each word in text (case-insensitive)."""
    words = re.findall(r"\b\w+\b", text.lower())
    return dict(Counter(words))

if __name__ == "__main__":
    sample_text = "The quick brown fox jumps over the lazy dog. The …
13 0 Open

Browse by section

Each section groups closely related Python snippets.

Dictionaries & sets — Python code examples

What you will find here

This page collects dictionaries & sets snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.