Glossary

Frozenset

A frozenset is the immutable counterpart of Python's set type. It supports all the same operations as a regular set — union, intersection, difference, symmetric difference, membership testing — but none of the mutating operations like .add(), .remove(), or .discard(). Because frozensets are immutable, they are hashable and can serve as dictionary keys or elements of other sets.

Frozensets are created with the frozenset() constructor, which accepts any iterable. There is no literal syntax for frozensets. The primary use case is when you need set semantics (uniqueness, fast membership testing, set algebra) but also need hashability — for instance, using a set of tags as a dictionary key, or building a set of sets.

Frozensets participate fully in set algebra with regular sets: frozenset({1, 2}) | {3} returns a frozenset. This makes them interchangeable with sets in read-only contexts. They are slightly more memory-efficient than sets because CPython can precompute and cache the hash value. In practice, frozensets appear less frequently than sets but are essential when immutability constraints demand them.

Related terms: Set, Immutable

Discussed in:

This site is currently in Beta. Please email Chris Paton (cpaton@gmail.com) with any suggestions, questions or comments.