Skip to main content

7.0 Python: frozenset

7.1 Python frozenset

frozenset is an immutable version of a set.

Like sets, it contains unique, unordered, unchangeable elements.

Unlike sets, elements cannot be added or removed from a frozenset.


7.2 Creating a forzenset

Use the frozenset() constructor to create a frozenset from any iterable.

7.2.1 Example

Create a frozenset and check its type:

x = frozenset({"apple", "banana", "cherry"})
print(x)
print(type(x))

7.3 Frozenset Methods

Being immutable means you cannot add or remove elements. However, frozensets support all non-mutating operations of sets.

MethodShortcutDescription
copy()Returns a shallow copy
difference()-Returns a new frozenset with the difference
intersection()&Returns a new frozenset with the intersection
isdisjoint()Returns whether two frozensets have an intersection
issubset()<= / <Returns True if this frozenset is a (proper) subset of another
issuperset()>= / >Returns True if this frozenset is a (proper) superset of another
symmetric_difference()^Returns a new frozenset with the symmetric differences
union()|Returns a new frozenset containing the union