Lesson 12
frozenset — Immutable Unique Collection
Learn the immutable version of set, create frozensets from different iterables, and understand why they can be used as dictionary keys.
Visual reference
What you will learn
- What
frozensetis and why it exists - Create frozensets from lists, sets, tuples, and strings
- Compare
setandfrozenset - Why
frozensetcan be a dictionary key - Understand complete immutability
Chapter 1 — What is a frozenset?
A frozenset is the immutable version of a set.
It shares set behavior:
- Stores unique values
- Removes duplicates automatically
- No indexing or slicing
- Unordered display
Unlike a normal set, nothing can be added or removed after creation.
Purpose (your notes): store multiple values (same or mixed types) with unique values when the collection must stay fixed.
Definition: an unordered collection of unique elements that cannot be modified.
Syntax: frozensetobj = frozenset(iterable) — built from set, list, tuple, str, bytes, bytearray, range, etc.
Note from your notes: frozenset works like set, but a set can add/remove (mutable operations) while still forbidding item assignment. frozenset forbids both — no add, remove, pop, discard, or fs[i] = ....
Chapter 2 — Why do we need it?
Some groups of values should never change during a program.
# Mutable — someone could weekend.add("Monday")
weekend = {"Saturday", "Sunday"}
# Immutable — fixed membership
weekend = frozenset({"Saturday", "Sunday"})Real-world examples
| Use case | Idea |
|---|---|
| Country codes | Constant unique codes |
| Permissions | Fixed role permission sets |
| Chemical elements | Immutable unique collections |
| Dictionary keys | Hashable unique groups |
Chapter 3 — Creating a frozenset
Use the built-in constructor on any iterable:
s = {10, 20, 30, 10, 20, 60}
fs = frozenset(s)
print(fs)
print(type(fs))frozenset({20, 10, 60, 30}) # order may vary
<class 'frozenset'>Duplicates disappear, same as set.
mixed = frozenset({10, "RS", 33.33, True})
print(mixed, len(mixed))Chapter 4 — Create from different iterables
| Source | Example |
|---|---|
| List | frozenset([1, 2, 2, 3]) |
| Tuple | frozenset((10, 20, 30)) |
| Set | frozenset({1, 2, 3}) |
| String | frozenset("APPLE") |
print(frozenset("APPLE"))frozenset({'A', 'P', 'L', 'E'}) # order may varyRepeated letters are removed automatically.
Chapter 5 — Empty frozenset
fs = frozenset()
print(fs)
print(len(fs))frozenset()
0Same length 0 as an empty set(), but immutable for its lifetime.
Chapter 6 — No indexing
Like set, frozensets are unordered and not subscriptable:
fs = frozenset({10, 20, 30})
# print(fs[0])
# TypeError: 'frozenset' object is not subscriptable
# fs[0:3]
# TypeError: 'frozenset' object is not subscriptableElements have no fixed position.
Chapter 7 — Complete immutability
A set allows mutation via methods:
s = {10, 20}
s.add(30)A frozenset does not:
fs = frozenset({10, 20})
# fs.add(30)
# AttributeError: 'frozenset' object has no attribute 'add'
# fs.remove(10)
# AttributeError: 'frozenset' object has no attribute 'remove'
# fs[0] = 23
# TypeError: 'frozenset' object does not support item assignment
# del fs[0]
# TypeError: 'frozenset' object doesn't support item deletionYou can delete the variable name with del fs — that removes the binding, not an element inside the frozenset.
Chapter 8 — Why is frozenset hashable?
Dictionary keys must be hashable — their value must not change while they live in the dict.
Invalid — mutable set:
# d = {{1, 2}: "pair"}
# TypeError: unhashable type: 'set'Valid — frozenset:
d = {
frozenset({1, 2}): "pair",
}
print(d){frozenset({1, 2}): 'pair'}Because the frozenset cannot change, Python can compute a stable hash and use it as a key.
Membership still works: 10 in fs (like sets).
Chapter 9 — set vs frozenset
| Feature | set |
frozenset |
|---|---|---|
| Unique values | Yes | Yes |
| Duplicates removed | Yes | Yes |
| Unordered display | Yes | Yes |
| Index / slice | No | No |
| Mutable (add/remove) | Yes | No |
.add() / .remove() |
Yes | No |
| Dictionary key | No (unhashable) | Yes (hashable) |
Practice exercises
1. Create from a list
fs = frozenset([1, 2, 2, 3])
print(fs)2. Try adding a value
fs = frozenset({1, 2, 3})
# fs.add(4) # AttributeError3. Use as a dictionary key
data = {
frozenset({1, 2}): "pair",
}
print(data)Quick knowledge check
-
How is frozenset similar to set?
Both store unique elements and are unordered (no indexing). -
Can you call
.add()on a frozenset?
No — it is immutable. -
How do you create an empty frozenset?
frozenset().
Summary
frozensetis an immutableset— unique values only, duplicates dropped.- No indexing, no add/remove, no item assignment or element deletion.
frozenset()creates an empty immutable collection.- Hashable → safe as a dictionary key when you need a fixed set of values.
Next lesson: dict — key–value mapping (keys, values, and fast lookup).
Loading playground…