Nearby lessons

31 of 108

Python - 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

  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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
frozenset({40, 10, 20, 30})

Duplicate Values are Not Allowed

Like a set, a frozenset automatically removes duplicate values.

Example - Duplicate Removal

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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
Python 3 Runtime
Loading Editor...
Output Preview
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
Python 3 Runtime
Loading Editor...
Output Preview
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
Python 3 Runtime
Loading Editor...
Output Preview
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
Python 3 Runtime
Loading Editor...
Output Preview
AttributeError: 'frozenset' object has no attribute 'add'

Example - remove() is Not Supported

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
AttributeError: 'frozenset' object has no attribute 'remove'

Iterating FrozenSet Elements

We can iterate through a frozenset using a for loop.

Example - Iteration

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10
20
30

Membership Operators

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

  • in
  • not in

Example - Membership Operators

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
True
False

Length of FrozenSet

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

Example - len() Function

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
3

Set Operations Supported by FrozenSet

A frozenset supports mathematical set operations.

Example - Union Operation

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
frozenset({40, 10, 20, 30})

Example - Intersection Operation

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
frozenset({20, 30})

Example - Difference Operation

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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
Python 3 Runtime
Loading Editor...
Output Preview
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()

🧠 Test Your Knowledge

8 Questions

Progress: 0 / 8
Keep Going!Python - Dictionaries