Python frozenset() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Built-in type

Definition

In Python, frozenset(iterable) creates an immutable set: it holds unique, hashable items like a set, but it has no add(), remove(), or other methods that change it.

Because it cannot change, a frozenset is hashable. That means it can be used as a dict key or stored inside another set, which a normal set cannot. Order does not matter: frozenset([1, 2]) == frozenset([2, 1]).

frozenset is a built-in type (a class) that you call like a function: each call creates a new frozenset object. Like every built-in, it lives in the builtins module and is always available without an import.

Syntax

Pass any iterable of hashable items.

python
fixed = frozenset(iterable)
empty = frozenset()
CallWhat it does
frozenset("aab")The unique letters a and b.
frozenset([1, 2]) == {1, 2}True: equal to a set with the same items.
{frozenset([1, 2]): "x"}A frozenset as a dict key.
a | bUnion; returns a new frozenset.

All read-only set operations work: |, &, -, ^, issubset(), in, and len().

Library declarations
python
frozenset(iterable=(), /)
# class frozenset: an immutable, hashable built-in type

Parameters

ParameterDescription
iterableOptional. An iterable of hashable items. Default: empty.

Return Value

Return ValueDescription
frozensetA new immutable set with the unique items.

Python Examples

Example 1 — A Read-Only Set of Vowels

Create a frozenset, test membership, and try to add an item.

python
vowels = frozenset("aeiou")

print(len(vowels), "e" in vowels, "y" in vowels)
print(sorted(vowels))

try:
    vowels.add("y")
except AttributeError:
    print("frozenset has no add() method")

How It Works

  1. Built from a string. Each vowel becomes one item of the frozenset.
  2. Fast lookups. in works exactly as with a normal set.
  3. Stable display. sorted() lists the vowels alphabetically, whatever the internal order.
  4. No mutating methods. add is not defined, so Python raises AttributeError.

Example 2 — Order-Independent Dict Keys

Store distances between city pairs so that the order of the two cities does not matter.

python
routes = {
    frozenset(["NYC", "BOS"]): 306,
    frozenset(["NYC", "DC"]): 362,
}
print(routes[frozenset(["BOS", "NYC"])])

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

How It Works

  1. Hashable key. A frozenset of two cities works as a dict key.
  2. Order ignored. Looking up BOS-NYC finds the entry stored as NYC-BOS.
  3. Set of sets. {1, 2} and {2, 1} are equal, so seen keeps only two frozensets.
  4. Equal to a set. A frozenset compares equal to a normal set with the same items.

Example 3 — Operations Return New frozensets

Combine frozensets, check the result type, and compare hashes of equal sets.

python
a = frozenset([1, 2, 3])
b = frozenset([3, 4])

union = a | b
print(type(union).__name__, sorted(union))
print(sorted(a & b), sorted(a - b))
print(type(a | {9}).__name__)
print(hash(a) == hash(frozenset([3, 2, 1])))

How It Works

  1. New objects. a | b creates a new frozenset; a and b stay unchanged.
  2. Intersection and difference. They share 3; a - b leaves 1 and 2.
  3. Left operand decides. Mixing with a plain set still returns a frozenset when the frozenset is on the left.
  4. Equal means equal hash. Two frozensets with the same items always have the same hash(), whatever order they were built in.

Frequently Asked Questions

A set is mutable and unhashable. A frozenset is immutable and hashable, so it can be a dict key or an item in another set.
Use frozensets for the inner sets: {frozenset({1, 2}), frozenset({3})}.
No. Create a new one instead: fs | {new_item}.
No. Like set, it has no guaranteed order. Use sorted() when you need a stable order.
For fixed collections such as allowed values, for order-independent dict keys, and for sets stored inside other sets.

Next: getattr()

Read an attribute by name, with an optional default.

getattr() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful