Introduction
Up to this point, we have already covered three major Python data structures: list, tuple, and set. Each of them addresses different requirements depending on whether you need ordering, mutability, or uniqueness. Before learning frozen sets, it is important to recall the core behavior of these three data types so the concept of a frozen set becomes easier to understand.
Lists allow duplicates, preserve order, and are mutable. Tuples allow duplicates, preserve order, but are immutable. Sets do not allow duplicates, do not preserve order, and are mutable. Frozen sets extend the concept of sets by adding immutability. Understanding frozen sets helps in cases where you want a collection of unique items, but that collection must remain constant throughout the execution of your program.
Quick Recap of Previously Learned Data Types
List
- Duplicates are allowed
- Order is preserved
- Indexing and slicing are supported
- Mutable and changeable after creation
Lists are useful when your data changes frequently and you care about the element positions.
Tuple
- Duplicates are allowed
- Order is preserved
- Indexing and slicing are supported
- Immutable and cannot be changed after creation
A tuple is essentially a read-only version of a list.
Set
- Duplicates are not allowed
- Order is not preserved
- Indexing and slicing are not supported
- Mutable and elements can be added or removed
Sets are useful when you need unique elements and order does not matter.
What Is a Frozen Set?
A frozen set is the immutable version of a set.
The meaning of the word frozen itself gives the idea. Something frozen cannot change shape. Similarly, once you create a frozen set, you cannot modify its contents. A frozen set maintains the same rules as a normal set regarding uniqueness and lack of ordering, but you lose the ability to add or remove elements.
A frozen set is exactly the same as a set except for one characteristic: immutability.
Example: Behavior of a Normal Set
Consider a regular set:
s = {10, 20, 30, 40}
You can freely modify this set:
s.add(50) s.remove(30) print(s)
The printed output will contain 10, 20, 40, 50, but the order is unpredictable. The main point is that a set allows adding and removing elements because it is mutable.
Creating a Frozen Set
If you already have a set, you can convert it to a frozen set using the built-in frozen set function:
s = {10, 20, 30, 40}
fs = frozen_set(s)
To verify:
print(type(fs))
Output:
<class 'frozenset'>
A frozen set looks similar when printed:
frozenset({40, 10, 20, 30})
The order is not guaranteed. This is the same behavior as a normal set regarding ordering, but immutability makes the frozen set behave differently when you try to modify it.
Frozen Set Is Immutable
Once you create a frozen set you cannot modify it.
Attempting to add an element
fs.add(50)
This results in:
AttributeError: 'frozenset' object has no attribute 'add'
Attempting to remove an element
fs.remove(30)
This results in:
AttributeError: 'frozenset' object has no attribute 'remove'
These errors occur because frozen sets do not provide methods related to modification. They only store values; they do not change once created.
Differences Between a Set and a Frozen Set
Mutability
- Set is mutable
- Frozen set is immutable
Methods
- Set supports add and remove
- Frozen set does not support any modifying methods
Use Cases
Sets are used when data evolves during runtime.
Frozen sets are used when data should remain fixed but must still follow set rules:
- No duplicates
- No ordering
Frozen sets are also hashable because they are immutable. This allows frozen sets to be used as dictionary keys or elements inside another set, which is impossible with normal sets.
Difference Between Tuple and Frozen Set
Even though both are immutable, they differ in several fundamental ways.
Ordering
- Tuple preserves order
- Frozen set does not preserve order
Duplicates
- Tuple allows duplicates
- Frozen set does not allow duplicates
Indexing and Slicing
- Tuple supports indexing and slicing
- Frozen set does not support indexing or slicing
Nature of Collection
- Tuple behaves like a fixed sequence
- Frozen set behaves like a fixed mathematical set
Although both are immutable, they belong to different categories of data structures. A tuple is sequence-oriented, whereas a frozen set is membership-oriented.
Practical Usage of Frozen Sets
Frozen sets are not as commonly used as lists, sets, or dictionaries. However, they become useful in these situations:
- When you need an immutable set of unique values
- When you want to store a set inside another set
- When you want to use a set as a dictionary key
- When you want to ensure data integrity by preventing accidental changes
For example, consider system configuration values, fixed permission sets, or categories that should never change once defined.
Summary
A frozen set is the immutable form of a set. It follows the same rules as a normal set regarding uniqueness and lack of ordering but cannot be modified after creation. This immutability allows frozen sets to be used in situations where normal sets cannot be used, such as dictionary keys or elements inside another set. A frozen set differs from a tuple because a tuple preserves order and allows duplicates, while a frozen set does neither. It also differs from a normal set because a frozen set is not modifiable.
