Nearby lessons

25 of 159

Python - FrozenSet Data Type

📌 What You Will Learn
  • 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

  1. Duplicate values are not allowed.
  2. Insertion order is not preserved.
  3. Heterogeneous values are allowed.
  4. Immutable.
  5. Indexing is not supported.

Creating a FrozenSet

We can create a frozenset object using the frozenset() function.

Example - Creating a FrozenSet

🐍Code Cell
1s = {10, 20, 30, 40}
2 
3fs = frozenset(s)
4 
5print(fs)
6print(type(fs))
Output
frozenset({40, 10, 20, 30})

Duplicate Values are Not Allowed

Like a set, a frozenset automatically removes duplicate values.

Example - Duplicate Removal

🐍Code Cell
1fs = frozenset([10, 20, 10, 30])
2 
3print(fs)
Output
frozenset({10, 20, 30})

Insertion Order is Not Preserved

A frozenset does not preserve the insertion order of elements.

Example - Order is Not Preserved

🐍Code Cell
1fs = frozenset([5, 10, 15, 20])
2 
3print(fs)
Output
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

🐍Code Cell
1fs = frozenset([10, 10.5, "durga", True])
2 
3print(fs)
Output
frozenset({True, 10, 10.5, 'durga'})

Indexing is Not Supported

Since insertion order is not preserved, indexing is not supported.

Example - Invalid Indexing

🐍Code Cell
1fs = frozenset([10, 20, 30])
2 
3print(fs[0])
Output
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

🐍Code Cell
1fs = frozenset([10, 20, 30])
2 
3fs.add(40)
Output
AttributeError: 'frozenset' object has no attribute 'add'

Example - remove() is Not Supported

🐍Code Cell
1fs = frozenset([10, 20, 30])
2 
3fs.remove(10)
Output
AttributeError: 'frozenset' object has no attribute 'remove'

Iterating FrozenSet Elements

We can iterate through a frozenset using a for loop.

Example - Iteration

🐍Code Cell
1fs = frozenset([10, 20, 30])
2 
3for i in fs:
4 print(i)
Output
10
20
30

Membership Operators

We can check whether an element exists in a frozenset using:

  • in
  • not in

Example - Membership Operators

🐍Code Cell
1fs = frozenset([10, 20, 30])
2 
3print(20 in fs)
4print(50 in fs)
Output
True
False

Length of FrozenSet

Use the len() function to find the number of elements in a frozenset.

Example - len() Function

🐍Code Cell
1fs = frozenset([10, 20, 30])
2 
3print(len(fs))
Output
3

Set Operations Supported by FrozenSet

A frozenset supports mathematical set operations.

Example - Union Operation

🐍Code Cell
1fs1 = frozenset([10, 20])
2fs2 = frozenset([30, 40])
3 
4print(fs1 | fs2)
Output
frozenset({40, 10, 20, 30})

Example - Intersection Operation

🐍Code Cell
1fs1 = frozenset([10, 20, 30])
2fs2 = frozenset([20, 30, 40])
3 
4print(fs1 & fs2)
Output
frozenset({20, 30})

Example - Difference Operation

🐍Code Cell
1fs1 = frozenset([10, 20, 30])
2fs2 = frozenset([20])
3 
4print(fs1 - fs2)
Output
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

🐍Code Cell
1permissions = frozenset(["read", "write"])
2 
3print(permissions)
Output
frozenset({'read', 'write'})

Important Notes

  • frozenset is 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

  • frozenset is the immutable version of set.
  • 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()
📝 Key Takeaways
  • 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

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8