Reference library

Dictionaries & sets

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

9 matches
Dictionaries & sets easy

Count Words in Python with Dictionaries and Sets

Text analysis example that counts total words, finds unique words with a set, and tallies character frequencies with a dictionary.

dictionaries sets text-processing
Python
def analyze_text(text: str) -> dict:
    """Count words, find unique words, and show common characters."""
    words = text.lower().split()
    word_count = len(words)
    unique_words = set(words)
    char_counts = {}
    
    for word in words:
        for char in word:
            if char.isalpha():
               …
13 0 Open
Dictionaries & sets easy

How to Count Tags with Sets and Dictionaries in Python

Count tag frequencies and collect unique tags from a list of dictionaries using Counter and sets in Python.

collections counter sets
Python
from collections import Counter
import json


def count_tags(entries):
    """Count tag frequencies across a list of entry dicts, using sets/dicts."""
    tag_counter = Counter()
    all_tags = set()
    for entry in entries:
        tags = set(entry["tags"])
        all_tags.update(tags)
        tag_counter.update(ta…
11 0 Open
Dictionaries & sets easy

How to Count Word Frequencies in Python

Count how often each word appears in a string and list the unique words using Python dictionaries and sets.

dictionaries sets text-processing
Python
def text_processor(text):
    words = text.lower().split()
    word_count = {}
    for word in words:
        word_count[word] = word_count.get(word, 0) + 1
    unique_words = set(words)
    return word_count, unique_words

if __name__ == "__main__":
    sample_text = "The quick brown fox jumps over the lazy dog and t…
14 0 Open
Dictionaries & sets easy

How to Count Word Frequencies in Python with Counter and Sets

This code processes a text string by lowercasing, splitting into words, counting frequencies with Counter, and extracting unique and sorted word lists using sets.

counter sets text-processing
Python
from collections import Counter

def process_text(text):
    words = text.lower().split()
    word_counts = Counter(words)
    unique_words = set(words)
    sorted_words = sorted(unique_words)
    
    return {
        "total_words": len(words),
        "unique_words": len(unique_words),
        "word_frequencies": di…
12 0 Open
Dictionaries & sets easy

How to Find the Intersection of Permission Sets in Python

This code defines a function that takes a list of permission sets and returns a set containing only the permissions common to all sets, with a short-circuit for empty results.

sets intersection permissions
Python
from typing import Set

def intersect_permissions(permission_sets: list[Set[str]]) -> Set[str]:
    """
    Given a list of permission sets, return the common permissions
    present in every set.
    """
    if not permission_sets:
        return set()
    
    common = permission_sets[0]
    for perm_set in permissi…
12 0 Open
Dictionaries & sets easy

How to Group a List of Dictionaries by Key in Python

Group a list of dictionaries by a specified key field using dict.setdefault to build a dictionary of lists.

dictionaries grouping setdefault
Python
def group_by_key(records, key):
    grouped = {}
    for record in records:
        grouped.setdefault(record[key], []).append(record)
    return grouped

if __name__ == "__main__":
    data = [
        {"name": "Alice", "dept": "engineering"},
        {"name": "Bob", "dept": "sales"},
        {"name": "Carol", "dept"…
14 0 Open
Dictionaries & sets easy

How to Validate Text and Count Words in Python

Count word frequencies, find unique and repeated words in a text using Python dictionaries and sets for beginner text validation.

dictionaries sets text-processing
Python
def validate_text(text):
    words = text.lower().split()
    
    word_counts = {}
    for word in words:
        cleaned = word.strip('.,!?;:"\'')
        if cleaned:
            word_counts[cleaned] = word_counts.get(cleaned, 0) + 1
    
    unique_words = set(word_counts.keys())
    repeated_words = {word for word…
12 0 Open
Dictionaries & sets easy

How to count words and find unique words in Python

Build a beginner-friendly text processor that counts word frequencies, finds unique words, and identifies words with vowels using dictionaries and sets.

dictionary set text-processing
Python
def text_processor(text):
    words = text.lower().replace(",", "").replace(".", "").split()
    word_count = {}
    
    for word in words:
        word_count[word] = word_count.get(word, 0) + 1
    
    unique_words = set(words)
    vowels = set("aeiou")
    words_with_vowels = {word for word in unique_words if vowe…
12 0 Open
Dictionaries & sets easy

Text Processor with Dictionaries and Sets in Python

Build a simple text processor that counts word frequencies with a dictionary and tracks unique words with a set.

dictionary set word-count
Python
def analyze_text(text):
    words = text.lower().split()
    word_freq = {}
    unique_words = set()
    
    for word in words:
        clean_word = word.strip('.,!?;:')
        if clean_word:
            word_freq[clean_word] = word_freq.get(clean_word, 0) + 1
            unique_words.add(clean_word)
    
    return…
12 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.