Python frozenset(): Immutable and Hashable Set

frozenset() is a built-in that creates an immutable, hashable set-like object from an iterable. It keeps the uniqueness and mathematical set operations of a regular set, but it does not provide methods that modify its contents.

Because a frozenset is immutable and hashable, it can be used where a regular mutable set cannot, such as a dictionary key or an element inside another set.

Syntax

frozenset(iterable)

The iterable can be a list, tuple, set, string or another iterable. Every element stored in the frozenset must itself be hashable.

Create a frozenset from a List Top ↑

values = [1, 2, 3, 2]
frozen = frozenset(values)
print(frozen)

The duplicate 2 is stored only once. Printed order should not be treated as fixed.

frozenset Cannot Be Modified In Place Top ↑

frozen = frozenset({1, 2, 3})
frozen.add(4)

This raises AttributeError because frozenset has no mutating add() method.

Item assignment also fails:

frozen = frozenset([1, 2, 3])
frozen[1] = 8

This raises TypeError. A frozenset is also unordered, so positional assignment would not make sense.

Create a frozenset from a Tuple Top ↑

my_tuple = (1, 2, 3)
frozen = frozenset(my_tuple)
print(frozen)

The original tuple can still be rebound or extended independently; the frozenset remains unchanged.

my_tuple = (1, 2, 3)
frozen = frozenset(my_tuple)
my_tuple += (4, 5)
print(my_tuple)
print(frozen)

Set Operations Return New frozenset Objects Top ↑

Non-mutating set operations are supported and return new objects.

A = frozenset({1, 2, 3})
B = frozenset({3, 4, 5})
print(A | B)
print(A & B)
print(A - B)
print(A ^ B)

As with regular sets, displayed order is not guaranteed.

Use a frozenset as a Dictionary Key Top ↑

permissions = {
    frozenset({'read', 'write'}): 'editor',
    frozenset({'read'}): 'viewer',
}
print(permissions[frozenset({'write', 'read'})])
editor

This works because a frozenset is hashable. A regular set cannot be used as a dictionary key.

Use a frozenset Inside a Set Top ↑

groups = {
    frozenset({1, 2}),
    frozenset({3, 4}),
}
print(len(groups))
2

set vs frozenset Top ↑

Featuresetfrozenset
Unique elementsYesYes
Ordered/indexedNoNo
MutableYesNo
Hashable objectNoYes, when its elements are hashable
Can be a dictionary keyNoYes
Can be an element of another setNoYes
Mutating methods such as add/removeYesNo

Common Mistakes Top ↑

  • The correct name is frozenset, not fronzenset.
  • frozenset does not make an unhashable nested value valid; its elements still need to be hashable.
  • The + operator is not a set-union operator. Use | or union().
  • Operations such as A | B create a new frozenset; they do not mutate A.

When to Use frozenset Top ↑

  • Represent a fixed collection of unique values.
  • Use a set-like collection as a dictionary key.
  • Store a set-like collection inside another set.
  • Protect a collection from accidental mutation while retaining set operations.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer