Skip to contentSkip to navigation

Dictionaries and Sets

Dictionaries & Sets — cheat sheet

Fast key-value mapping and mathematical set operations in Python.

Dictionary basics

d = {"name": "Alice", "age": 25}
Define key-value pairs.
d["email"] = "a@b.com"
Insert new or update existing key.
val = d.get("role", "guest")
Safe lookup with default fallback.

Set operations

s = {1, 2, 3}
Set of unique items.
union = s1 | s2
Combine items from both sets.
common = s1 & s2
Intersection: items present in both.
All cheat sheets