Home

frozenset

Frozenset is an immutable, unordered collection of distinct elements in Python. It is the immutable counterpart to the built-in set type. Like a set, a frozenset cannot contain duplicates and supports membership testing, iteration, and standard set operations, but its contents cannot be changed after creation.

Creation and immutability: A frozenset is created with the frozenset() constructor, optionally with an iterable, e.g.,

Operations: Frozensets support union, intersection, difference, and symmetric_difference using operators |, &, -, and ^, or the corresponding methods. The

Limitations: A frozenset does not support in-place modification methods such as add, remove, discard, pop, or

Usage: Frozensets are useful when a fixed collection must be used as a dictionary key or as

frozenset([1,
2,
3]).
An
empty
frozenset
is
produced
by
frozenset().
Because
it
is
immutable,
a
frozenset
is
hashable
if
all
its
elements
are
hashable,
which
allows
it
to
be
used
as
a
dictionary
key
or
as
an
element
of
another
set.
results
of
these
operations
are
new
frozenset
objects.
Membership
tests
(x
in
f)
and
iteration
are
supported,
and
issubset/issuperset
comparisons
(<=,
>=)
are
available.
update.
Its
immutability
means
that
you
must
create
a
new
frozenset
to
reflect
any
change.
All
elements
of
a
frozenset
must
be
hashable
to
be
included.
an
element
of
a
set,
or
when
a
read-only
view
of
a
set
is
required.
They
are
typically
used
to
represent
fixed
sets
of
attributes,
categories,
or
configuration
flags.