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

Loading editor…

Duplicate Values are Not Allowed

Like a set, a frozenset automatically removes duplicate values.

Example - Duplicate Removal

Loading editor…

Insertion Order is Not Preserved

A frozenset does not preserve the insertion order of elements.

Example - Order is Not Preserved

Loading editor…

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

Loading editor…

Indexing is Not Supported

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

Example - Invalid Indexing

Loading editor…

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

Loading editor…

Example - remove() is Not Supported

Loading editor…

Iterating FrozenSet Elements

We can iterate through a frozenset using a for loop.

Example - Iteration

Loading editor…

Membership Operators

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

  • in
  • not in

Example - Membership Operators

Loading editor…

Length of FrozenSet

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

Example - len() Function

Loading editor…

Set Operations Supported by FrozenSet

A frozenset supports mathematical set operations.

Example - Union Operation

Loading editor…

Example - Intersection Operation

Loading editor…

Example - Difference Operation

Loading editor…

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

Loading editor…

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

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.