Python Frozenset
In this page:
frozen = frozenset(iterable)
Creating a Frozenset
frozenset(iterable) builds an immutable set from any iterable -- a list, a tuple, a regular set, or a string of characters -- producing a collection that behaves like a set for reading purposes, but permanently locks its contents at creation time.
Example: Creating a Frozenset
numbers = frozenset([1, 2, 2, 3])
print(numbers)
Frozensets Are Immutable
Once created, a frozenset cannot be changed in any way -- there is no add(), remove(), discard(), pop(), or clear() method available on it, since all of these would modify its contents, which directly contradicts the entire point of a frozenset.
Example: Frozensets Are Immutable
numbers = frozenset([1, 2, 3])
try:
numbers.add(4) # frozenset has no add() method
except AttributeError as e:
print("Error:", e)
Frozensets as Dictionary Keys
Because a frozenset is immutable and therefore hashable, it can be used as a dictionary key or stored as an element inside another set -- both operations that raise a TypeError with a regular, mutable set, since Python requires dictionary keys and set elements to never change after being added.
Example: Frozensets as Dictionary Keys
key = frozenset([1, 2]) # frozensets are hashable, so they can be dict keys
data = {key: "pair one-two"}
print(data[frozenset([1, 2])]) # an equal frozenset looks up the same entry
Set Operations on Frozensets
Frozensets support the exact same read-only set operations as regular sets -- union (|), intersection (&), difference (-), and membership testing (in) -- since none of these operations modify the frozenset itself, only combine it with something else to produce a new result.
Example: Set Operations on Frozensets
a = frozenset([1, 2, 3])
b = frozenset([2, 3, 4])
print(a | b) # union, produces a new frozenset
print(a & b) # intersection, also produces a new frozenset
When to Use frozenset vs set
Choose a regular set when the collection genuinely needs to grow or shrink over the program's lifetime; choose frozenset specifically when you need an immutable, hashable set-like value -- protecting a fixed collection of constants from accidental modification, or using it as a dictionary key or set element.
Example: When to Use frozenset vs set
mutable_set = {1, 2, 3}
mutable_set.add(4) # regular set can grow
immutable_set = frozenset([1, 2, 3]) # frozenset can't be changed after creation
print(mutable_set, immutable_set)
- Trying to call .add() or .remove() on a frozenset, which raises an AttributeError since frozensets do not support any method that would modify their contents.
- Using a regular set as a dictionary key or as an element inside another set, which raises a TypeError since regular sets are unhashable -- frozenset solves exactly this problem.
- Assuming frozenset() creates a copy that automatically stays in sync with the original set it was built from -- it is actually an independent, frozen snapshot at the time of creation.
- frozenset(iterable) creates an immutable set from any iterable, supporting the same read-only operations (membership tests, unions, intersections) as a regular set.
- Because a frozenset is immutable and hashable, it can be used as a dictionary key or as an element inside another set, unlike a regular set.
- Unlike a regular set, a frozenset has no methods that modify it in place -- no add(), remove(), or similar mutating methods exist.
frozenset has been part of core Python since Python 2.4, and works identically across every Python 3 version.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: