all Python 3Built-in functionsterm
frozenset
types
An immutable set. Supports every set operation (union, intersection and so on) but has no add() or remove(). Hashable — it can be used as a dictionary key.
Syntax
frozenset(iterable)
Use frozenset when a set has to become a dictionary key or an element of another set — a plain set is rejected there because it is unhashable. There is no literal syntax for it, only the constructor call: braces always build a set, and empty braces build a dict. A trap: frozenset({1,2}) == {1,2} is True, yet you still cannot look the entry up with a plain set — d[{1,2}] raises TypeError, because the key has to be hashed first.
Examples
fs = frozenset([1,2,3,2,1])
print(fs) # → frozenset({1, 2, 3})
print(sorted(frozenset('hello'))) # → ['e', 'h', 'l', 'o'] (порядок множества не определён, поэтому сортируем)
d = {frozenset([1,2]): 'value'}
print(d) # → {frozenset({1, 2}): 'value'}
print(1 in fs) # → True
print(fs & frozenset([2,3,4])) # → frozenset({2, 3})
See also
Open in the full glossarydocs.python.orgReport an inaccuracy