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.
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
| Parameter | What it is |
|---|---|
iterableoptional | 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
nums = frozenset([3, 1, 2, 3, 1])
print(nums)
print(len(nums))
print(2 in nums)frozenset({1, 2, 3})
3
TrueDuplicates 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.
distances = {
frozenset(["Paris", "Lyon"]): 465,
frozenset(["Paris", "Nice"]): 930,
}
print(distances[frozenset(["Lyon", "Paris"])])
print(distances[frozenset(["Nice", "Paris"])])465 930
3Set operations still work
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))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.
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))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.
tags = frozenset({1, 2})
try:
tags.add(3)
except AttributeError:
print("AttributeError: frozensets cannot change")
tags = tags | {3}
print(tags)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.
try:
groups = {{1, 2}, {3}}
except TypeError:
print("TypeError: a set cannot hold sets")
groups = {frozenset({1, 2}), frozenset({3})}
print(len(groups))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.