A set is a mutable, unordered collection of unique hashable items, while a frozenset is its immutable counterpart. Because a frozenset cannot change after creation, it is hashable, so you can use it as a dictionary key or store it inside another set. A regular set cannot be used that way.
Both types give you fast membership tests, duplicate removal and the usual set algebra (union, intersection, difference). The choice comes down to one question: does the collection need to change after you build it, or does it need to be hashable?
| Feature | set | frozenset |
|---|---|---|
| Mutable | Yes | No |
| Hashable | No | Yes (if all elements are hashable) |
| Can be a dict key or set element | No | Yes |
add(), remove(), discard(), pop(), clear() | Yes | No |
union(), intersection(), difference() | Yes, return a new set | Yes, return a new frozenset |
| Literal syntax | {1, 2, 3} | None, use frozenset([1, 2, 3]) |
| Typical use | Working collection that grows or shrinks | Constant lookup table, composite key |
Both types require their elements to be hashable. A list or a plain set cannot be an element of either type, but a frozenset can.
A set supports in-place changes. A frozenset only has the read-only methods, so calling add() on it raises AttributeError because the method does not exist.
Use discard() when a missing element is acceptable and remove() when you want a KeyError to flag a bug. pop() removes and returns an arbitrary element.
Python needs a stable hash for anything used as a dictionary key or a set member. A mutable set could change its contents, and therefore its hash, so Python refuses to hash it.
This is the main reason frozenset exists. It lets you model an unordered pair or group as a key, for example an undirected edge in a graph, where {"A", "B"} and {"B", "A"} are the same key. You can also build a set of sets:
Operators and methods work on both types. When you mix them, the result takes the type of the left operand.
Equality ignores the type, so {1, 2} == frozenset({1, 2}) is True. Only a set also has the in-place variants such as update(), intersection_update() and the |= operator.
Choose a set when you build up a collection incrementally, remove duplicates from data, or track visited items during a search. Choose a frozenset when the value is a constant, or when it must be hashable.
Performance is almost identical. Both are hash tables with average O(1) membership tests. A frozenset can be cached or reused safely, and sharing one between threads or functions cannot cause accidental modification, which is often the practical benefit.
Each conversion creates a new object, so the copies are independent.
No. Elements must be hashable and a set is not. A frozenset can contain other frozenset objects, numbers, strings and tuples of hashable items.
No. You must call frozenset(iterable). An empty literal {} creates a dictionary, so use set() for an empty set and frozenset() for an empty frozenset.
No. Neither type guarantees iteration order, unlike dict, which keeps insertion order. Use sorted() when you need predictable output.
Not in a way that matters for lookups. Both use hashing. Pick based on mutability and hashability, not speed.
A set is mutable and unhashable, and a frozenset is immutable and hashable. Use set for collections you modify and frozenset for constants, dictionary keys and members of other sets. Both support the same set algebra, and mixed operations return the type of the left operand.