Code Reference

Sets

Python · Reference cheat sheet

Sets

Python · Reference cheat sheet


📋 Overview

A set is an unordered collection of unique hashable elements. Use for membership tests, deduplication, and set algebra (union, intersection, difference). frozenset is the immutable, hashable variant.

🔧 Core concepts

OperationExample
Create\{1, 2\}, set(iterable), set() (not \{\})
Add / removes.add(x), s.discard(x), s.remove(x), s.pop()
Membershipx in s — average O(1)
Uniona | b, a.union(b)
Intersectiona & b, a.intersection(b)
Differencea - b, a.difference(b)
Symmetric diffa ^ b
Subseta <= b, a < b (proper)

Elements must be hashable. Insertion order is preserved for iteration in 3.7+ CPython, but do not treat sets as ordered sequences.

💡 Examples

Deduplicate and membership:

tags = ["py", "web", "py", "api"]
unique = set(tags)                 # {"py", "web", "api"}
print("web" in unique)             # True

Set algebra:

required = {"auth", "db", "cache"}
installed = {"auth", "db", "logs"}

missing = required - installed     # {"cache"}
extra = installed - required       # {"logs"}
both = required & installed        # {"auth", "db"}

Comprehension and frozenset:

evens = {n for n in range(10) if n % 2 == 0}
key = frozenset({"a", "b"})
groups: dict[frozenset[str], int] = {key: 1}

Update in place:

s = {1, 2}
s |= {2, 3}      # {1, 2, 3}
s &= {2, 3, 4}   # {2, 3}

⚠️ Pitfalls

  • \{\} creates an empty dict, not a set — use set().
  • Unhashable elements (list, dict, set) raise TypeError.
  • remove raises KeyError if missing; discard does not.
  • Set equality ignores order; do not rely on iteration order for logic.
  • Prefer frozenset when you need a set as a dict key or nested in another set.

On this page