Nearby lessons
31 of 108Python - FrozenSet Data Type
Introduction
The frozenset data type is almost the same as the set data type.
The main difference is that a frozenset is immutable.
Once a frozenset object is created, its elements cannot be added, removed, or modified.
Difference Between set and frozenset
| set | frozenset |
|---|---|
| Mutable | Immutable |
add() is supported. |
add() is not supported. |
remove() is supported. |
remove() is not supported. |
Features of FrozenSet Data Type
- Duplicate values are not allowed.
- Insertion order is not preserved.
- Heterogeneous values are allowed.
- Immutable.
- Indexing is not supported.
Creating a FrozenSet
We can create a frozenset object using the frozenset() function.
Example - Creating a FrozenSet
main.py
frozenset({40, 10, 20, 30})Duplicate Values are Not Allowed
Like a set, a frozenset automatically removes duplicate values.
Example - Duplicate Removal
main.py
frozenset({10, 20, 30})Insertion Order is Not Preserved
A frozenset does not preserve the insertion order of elements.
Example - Order is Not Preserved
main.py
frozenset({10, 20, 5, 15})Important Note
The output order may be different each time you run the program.
This is because a frozenset does not preserve insertion order.
Heterogeneous Values Allowed
A frozenset can store different types of values.
Example - Heterogeneous Values
main.py
frozenset({True, 10, 10.5, 'durga'})Indexing is Not Supported
Since insertion order is not preserved, indexing is not supported.
Example - Invalid Indexing
main.py
TypeError: 'frozenset' object is not subscriptable
Why Indexing is Not Supported?
Indexing depends on the position of elements.
Since a frozenset has no fixed order, indexing cannot be used.
FrozenSet is Immutable
Once a frozenset object is created, its elements cannot be modified.
Example - add() is Not Supported
main.py
AttributeError: 'frozenset' object has no attribute 'add'
Example - remove() is Not Supported
main.py
AttributeError: 'frozenset' object has no attribute 'remove'
Iterating FrozenSet Elements
We can iterate through a frozenset using a for loop.
Example - Iteration
main.py
10 20 30
Membership Operators
We can check whether an element exists in a frozenset using:
innot in
Example - Membership Operators
main.py
True False
Length of FrozenSet
Use the len() function to find the number of elements in a frozenset.
Example - len() Function
main.py
3
Set Operations Supported by FrozenSet
A frozenset supports mathematical set operations.
Example - Union Operation
main.py
frozenset({40, 10, 20, 30})Example - Intersection Operation
main.py
frozenset({20, 30})Example - Difference Operation
main.py
frozenset({10, 30})Real World Usage of FrozenSet
A frozenset is useful when:
- Data should not change.
- A unique collection is required.
- A read-only set is needed.
Example - User Permissions
main.py
frozenset({'read', 'write'})Important Notes
frozensetis immutable.- Duplicate values are not allowed.
- Insertion order is not preserved.
- Indexing is not supported.
- Mathematical set operations are supported.
Difference Between set and frozenset
| Feature | set | frozenset |
|---|---|---|
| Mutable | Yes | No |
| add() Supported | Yes | No |
| remove() Supported | Yes | No |
| Duplicates Allowed | No | No |
| Insertion Order Preserved | No | No |
Key Points
frozensetis the immutable version ofset.- Duplicate values are removed automatically.
- Insertion order is not preserved.
- Indexing is not supported.
- Supports iteration, membership operators,
len(), and mathematical set operations. - Useful for storing read-only collections of unique values.
Quick Summary
| Feature | Description |
|---|---|
| Data Type | frozenset |
| Mutable | No |
| Duplicates Allowed | No |
| Insertion Order | Not Preserved |
| Indexing Supported | No |
| Growable | No |
| Created Using | frozenset() |