Python Frozenset
In this page:
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.
Note: Pass any existing set, list, or tuple directly into frozenset() to create an immutable snapshot of it, without changing the original.
Warning: frozenset() with no arguments creates an empty frozenset, but there is no literal syntax shortcut for it the way {} would be ambiguous for an empty dictionary versus an empty set.
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.
Note: If you find yourself needing to modify a frozenset's contents, that is a strong signal you actually want a regular, mutable set instead.
Warning: Calling a mutating method (like .add()) on a frozenset raises an AttributeError immediately, since that method simply does not exist on the frozenset type.
Example: Frozensets Are Immutable
numbers = frozenset([1, 2, 3])
try:
numbers.add(4)
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.
Note: Reach for a frozenset specifically when you need a set-like value to serve as a dictionary key or live inside another set.
Warning: Attempting to use a regular set (not a frozenset) as a dictionary key raises "TypeError: unhashable type: set" immediately.
Example: Frozensets as Dictionary Keys
key = frozenset([1, 2])
data = {key: "pair one-two"}
print(data[frozenset([1, 2])])
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.
Note: Use frozensets exactly like regular sets for any read-only operation (membership checks, unions, intersections) -- the immutability only restricts modification, not reading or combining.
Warning: A union or intersection between two frozensets produces a new frozenset, not a regular set -- the immutability propagates through these combining operations.
Example: Set Operations on Frozensets
a = frozenset([1, 2, 3])
b = frozenset([2, 3, 4])
print(a | b)
print(a & b)
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.
Note: Default to a regular set unless you have a specific reason (immutability, hashability) to reach for frozenset instead.
Warning: Using frozenset everywhere out of caution, even for collections that genuinely need to change over time, forces awkward "create a new frozenset every time" workarounds instead of simple in-place updates.
Example: When to Use frozenset vs set
mutable_set = {1, 2, 3}
mutable_set.add(4)
immutable_set = frozenset([1, 2, 3])
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: