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
| Parameter | Description |
|---|---|
iterable optional |
The items; duplicates are dropped. |
Return value
A new frozenset.
Examples
vowels = frozenset("aeiou")
print("e" in vowels, "z" in vowels)
print(len(frozenset([1, 1, 2, 3])))
Output
True False 3
routes = {frozenset({"A", "B"}): 12, frozenset({"B", "C"}): 7}
print(routes[frozenset({"B", "A"})])
Output
12
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
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
set()· Create a set: an unordered collection of unique items with fast membership tests and set operations.frozendict()· Create an immutable, hashable dictionary (new in Python 3.15). It can be a dict key or set member and can't be changed.tuple()· Create a tuple: an ordered, immutable sequence. Tuples are hashable when their items are, so they can be dict keys.- Python Sets · tutorial