Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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.

Beginner12 min

Visual reference

Lock — frozen, cannot change in placeLock — frozen, cannot change in place
Immutable data architectureImmutable data architecture
Frozen assets — values that stay fixedFrozen assets — values that stay fixed

What you will learn

  • What frozenset is and why it exists
  • Create frozensets from lists, sets, tuples, and strings
  • Compare set and frozenset
  • Why frozenset can 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.

Frozen • unique • immutable

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.

python
# 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:

python
s = {10, 20, 30, 10, 20, 60}

fs = frozenset(s)

print(fs)
print(type(fs))
text
frozenset({20, 10, 60, 30})   # order may vary
<class 'frozenset'>

Duplicates disappear, same as set.

python
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")
python
print(frozenset("APPLE"))
text
frozenset({'A', 'P', 'L', 'E'})   # order may vary

Repeated letters are removed automatically.

Chapter 5 — Empty frozenset

python
fs = frozenset()

print(fs)
print(len(fs))
text
frozenset()
0

Same length 0 as an empty set(), but immutable for its lifetime.

Chapter 6 — No indexing

Like set, frozensets are unordered and not subscriptable:

python
fs = frozenset({10, 20, 30})

# print(fs[0])
# TypeError: 'frozenset' object is not subscriptable

# fs[0:3]
# TypeError: 'frozenset' object is not subscriptable

Elements have no fixed position.

Chapter 7 — Complete immutability

A set allows mutation via methods:

python
s = {10, 20}
s.add(30)

A frozenset does not:

python
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 deletion

You can delete the variable name with del fs — that removes the binding, not an element inside the frozenset.

set: add/remove vs frozenset: read only

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:

python
# d = {{1, 2}: "pair"}
# TypeError: unhashable type: 'set'

Valid — frozenset:

python
d = {
    frozenset({1, 2}): "pair",
}

print(d)
text
{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

python
fs = frozenset([1, 2, 2, 3])
print(fs)

2. Try adding a value

python
fs = frozenset({1, 2, 3})
# fs.add(4)  # AttributeError

3. Use as a dictionary key

python
data = {
    frozenset({1, 2}): "pair",
}
print(data)

Quick knowledge check

  1. How is frozenset similar to set?
    Both store unique elements and are unordered (no indexing).

  2. Can you call .add() on a frozenset?
    No — it is immutable.

  3. How do you create an empty frozenset?
    frozenset().

Summary

  • frozenset is an immutable set — 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…