Reference library

Dictionaries & sets

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

2 matches
Dictionaries & sets easy

How to Diff Two Dicts in Python: Added, Removed, and Changed Keys

Compare two dictionaries and report added, removed, and changed keys using Python's set operations on dict keys.

dict diff set-operations
Python
def diff_dicts(old: dict, new: dict) -> dict:
    """Compare two dicts and report added, removed, and changed keys."""
    added = {k: new[k] for k in new.keys() - old.keys()}
    removed = {k: old[k] for k in old.keys() - new.keys()}

    common_keys = old.keys() & new.keys()
    changed = {k: (old[k], new[k]) for k …
15 0 Open
Dictionaries & sets easy

How to Find Symmetric Difference Between Two Python Sets

Compute elements unique to each set and build a flag dictionary showing membership across two Python sets.

sets set-operations symmetric-difference
Python
def symmetric_difference_with_flags(set_a, set_b):
    """Return elements in either set but not both, grouped by which set they came from."""
    only_in_a = set_a - set_b
    only_in_b = set_b - set_a
    
    print(f"Only in A: {only_in_a}")
    print(f"Only in B: {only_in_b}")
    print(f"Symmetric difference: {onl…
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.