Dictionary creation, safe lookups, updates, iteration and transforms, then the set algebra.
d = {"a": 1, "b": 2}
s = {1, 2, 3}
t = {3, 4}
pairs = [("x", 1), ("y", 2)]{}, dict(), dict(a=1, b=2)Empty, empty, and from keyword arguments.
dict(zip(["a", "b"], [1, 2]))From two parallel sequences.
dict(pairs)From an iterable of (key, value) pairs.
{k: v * 10 for k, v in pairs}Dict comprehension → {'x': 10, 'y': 20}.
dict.fromkeys(["a", "b"], 0)Same default for every key → {'a': 0, 'b': 0}.
from collections import defaultdict
groups = defaultdict(list)
groups["k"].append(1)Missing keys are created with list() on first access.
from collections import Counter
Counter("banana").most_common(2)Count anything hashable → [('a', 3), ('n', 2)].
d["a"]Value for a key; KeyError when missing.
d.get("z"), d.get("z", 0)None, or your default, when missing — no exception.
"a" in d, "z" not in dKey membership, O(1).
len(d)Number of keys.
list(d.keys()), list(d.values()), list(d.items())Keys, values, (key, value) pairs — live views until you list() them.
list(d)Iterating a dict yields its keys, in insertion order.
next(iter(d))First inserted key.
d["c"] = 3Insert or overwrite.
d.setdefault("tags", []).append("new")Insert a default only if the key is missing, then return the value.
d.update({"a": 10}, e=5)Merge in place; later values win.
d | {"z": 0}Merged copy (3.9+); d |= other merges in place.
{**d, "z": 0}Merged copy, the pre-3.9 way.
d.pop("a"), d.pop("zzz", None)Remove and return; the default avoids KeyError.
d.popitem()Remove and return the most recently inserted pair.
del d["b"]Delete a key; KeyError when missing.
for k, v in d.items():
print(k, v)The normal way to loop over a dict.
sorted(d)Keys in sorted order.
sorted(d.items(), key=lambda kv: kv[1], reverse=True)Pairs sorted by value, highest first.
max(d, key=d.get)The key with the largest value → 'b'.
{v: k for k, v in d.items()}Invert (values must be unique and hashable).
{k: v for k, v in d.items() if v > 1}Filter → {'b': 2}.
sum(d.values())→ 3.
d.get("x", {}).get("y")Safe nested lookup → None instead of KeyError.
dict(sorted(d.items()))A new dict with keys in sorted order.
{1, 2, 3}, set(), set([1, 1, 2])Literal / empty ({} is a dict!) / from an iterable → {1, 2}.
s.add(4); s.discard(99); s.remove(1)discard ignores a missing item, remove raises KeyError.
s | t, s & t, s - t, s ^ tUnion {1,2,3,4}, intersection {3}, difference {1,2}, symmetric difference {1,2,4}.
s.union(t), s.intersection(t), s.difference(t)Method forms accept any iterable, not only sets.
{2, 3} <= s, s.isdisjoint(t)Subset test → True; no common items → False.
s.update([5, 6]); s.pop()Add many; pop removes and returns an arbitrary item.
frozenset(s)Immutable, hashable set — usable as a dict key or inside another set.
len(set(xs := [1, 2, 2])) == len(xs)All unique? → False.
{x % 3 for x in range(10)}Set comprehension → {0, 1, 2}.
sorted(set("mississippi"))Unique characters in order → ['i', 'm', 'p', 's'].
Want the topic explained, not just listed? The Dictionaries lesson walks through it with graded exercises. Preparing for interviews? See the interview prep track.