Python 3 reference · Built-in Functions

Python frozenset() Function

Create an immutable set: unique items that can't be added or removed, so it is hashable and can be a dict key.

Python 3 Since 3.0

Syntax

frozenset(iterable=())

Parameters

ParameterDescription
iterable optional The items; duplicates are dropped.

Return value

A new frozenset.

Examples

Create and test membershipTry it yourself
vowels = frozenset("aeiou")
print("e" in vowels, "z" in vowels)
print(len(frozenset([1, 1, 2, 3])))

Output

True False
3
Use as a dictionary keyTry it yourself
routes = {frozenset({"A", "B"}): 12, frozenset({"B", "C"}): 7}
print(routes[frozenset({"B", "A"})])

Output

12
Set operations return new frozensetsTry it yourself
a, b = frozenset({1, 2, 3}), frozenset({2, 3, 4})
print(sorted(a & b), sorted(a | b), sorted(a - b))

Output

[2, 3] [1, 2, 3, 4] [1]

Version history

VersionChange
Python 3.0Added