Built-in function

frozenset()

Creates a frozenset: a set that can never change after it is made, so it can be a dictionary key or an item inside another set.

IntermediateEvery example below runs in your browser. Change the code and press Run.

frozenset() frozenset(iterable)

A frozenset works like a normal set(): it holds unique items in no particular order and supports union, intersection and fast in checks. The one difference is that it is frozen: it has no add(), remove() or clear().

Because it cannot change, Python can compute a fixed hash for it. That means a frozenset can be used where a normal set cannot: as a dictionary key, or as an item inside another set. A pair of cities, a group of tags or a combination of ingredients can then act as one key.

Like sets, frozensets have no fixed order. Small whole numbers usually print sorted, but strings appear in a different order from run to run, so use sorted() when you print them.

Parameters

ParameterWhat it is
iterable
optional
The items to put in: a list, string, set, range... Duplicates are dropped. With no argument you get an empty frozenset.

Return value

A new frozenset.

Examples

1Create a frozenset

Pythoneditable
nums = frozenset([3, 1, 2, 3, 1])
print(nums)
print(len(nums))
print(2 in nums)
Output
frozenset({1, 2, 3})
3
True

Duplicates disappear, exactly as in a normal set.

2A key made of two cities

The distance from Paris to Lyon is the same as from Lyon to Paris. A frozenset key ignores the order.

Pythoneditable
distances = {
    frozenset(["Paris", "Lyon"]): 465,
    frozenset(["Paris", "Nice"]): 930,
}
print(distances[frozenset(["Lyon", "Paris"])])
print(distances[frozenset(["Nice", "Paris"])])
Output
465
930

3Set operations still work

Pythoneditable
weekdays = frozenset({1, 2, 3, 4, 5})
my_days = {4, 5, 6}
print(weekdays & my_days)
print(weekdays | my_days)
print(sorted(weekdays - my_days))
Output
frozenset({4, 5})
frozenset({1, 2, 3, 4, 5, 6})
[1, 2, 3]

When you combine a frozenset with a normal set, the result has the type of the left-hand side.

4A set of sets

Two orders with the same toppings in a different order count as one.

Pythoneditable
orders = [["cheese", "ham"], ["ham", "cheese"], ["tuna"]]
unique = {frozenset(o) for o in orders}
print(len(unique), "different pizzas")
for combo in unique:
    if len(combo) > 1:
        print(sorted(combo))
Output
2 different pizzas
['cheese', 'ham']

Common mistakes

Trying to change a frozenset

Frozensets have no add() or remove(). Make a new one with | or -, or use a normal set if it needs to change.

Pythoneditable
tags = frozenset({1, 2})
try:
    tags.add(3)
except AttributeError:
    print("AttributeError: frozensets cannot change")
tags = tags | {3}
print(tags)
Output
AttributeError: frozensets cannot change
frozenset({1, 2, 3})

Putting normal sets inside a set

A normal set can change, so it cannot be hashed and cannot go inside another set or be a dict key. Freeze it first.

Pythoneditable
try:
    groups = {{1, 2}, {3}}
except TypeError:
    print("TypeError: a set cannot hold sets")
groups = {frozenset({1, 2}), frozenset({3})}
print(len(groups))
Output
TypeError: a set cannot hold sets
2

Questions people ask

What is the difference between set and frozenset?

Same items and operations, but a frozenset cannot be changed after it is created. In return it can be a dictionary key or a member of another set.

How do I convert a frozenset to a set or list?

set(fs) gives a changeable copy; sorted(fs) gives a sorted list.

Is a frozenset faster than a set?

Lookups with in are equally fast. The benefit is safety and hashability, not speed.

Spotted a mistake on this page? Tell us