Nearby lessons
25 of 159Python - FrozenSet Data Type
- Define the frozenset data type and how it differs from set
- Create a frozenset with the frozenset() function
- Explain why duplicates are removed and order is not preserved
- Understand why indexing is not supported in a frozenset
- Use iteration, membership operators, and len() on a frozenset
- Perform mathematical set operations on frozensets
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
Duplicate Values are Not Allowed
Like a set, a frozenset automatically removes duplicate values.
Example - Duplicate Removal
Insertion Order is Not Preserved
A frozenset does not preserve the insertion order of elements.
Example - Order is Not Preserved
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
Indexing is Not Supported
Since insertion order is not preserved, indexing is not supported.
Example - Invalid Indexing
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
Example - remove() is Not Supported
Iterating FrozenSet Elements
We can iterate through a frozenset using a for loop.
Example - Iteration
Membership Operators
We can check whether an element exists in a frozenset using:
innot in
Example - Membership Operators
Length of FrozenSet
Use the len() function to find the number of elements in a frozenset.
Example - len() Function
Set Operations Supported by FrozenSet
A frozenset supports mathematical set operations.
Example - Union Operation
Example - Intersection Operation
Example - Difference Operation
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
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() |
- A frozenset is the immutable version of a set
- Duplicate values are removed automatically from a frozenset
- Insertion order is not preserved, so indexing is not supported
- add() and remove() are not available on a frozenset
- A frozenset supports union, intersection, and difference operations
- A frozenset is useful for storing read-only collections of unique values