Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Class

frozenset

frozenset creates an immutable, hashable set of unique elements. It is useful as a dictionary key or as an element of another set when the collection itself must remain fixed.

How it works

Call frozenset with an iterable to create an immutable set of its elements. Because the contents cannot change, a frozenset is hashable and can serve as a dictionary key or as an element inside another set; it supports set operations but not mutating operations.

Syntax

class frozenset(iterable=(), /)

Parameters

NameTypeDescription
iterableiterableAn optional iterable whose elements are used to create the new frozenset; it defaults to an empty iterable.

Returns

A new frozenset object containing the elements taken from iterable.

Key points

  • Python provides frozenset as the immutable counterpart to the mutable set type.
  • Its contents cannot be changed after construction, and it can therefore participate in hash-based collections.
  • Use a frozenset as a dictionary key when the key represents an unordered, fixed collection of values.
  • A frozenset can be nested inside a set because it is hashable.

Common mistakes

  • Do not use mutating methods or in-place update operators; choose a new set operation when you need a changed value.

Example: Use a frozenset as a dictionary key

allowed = frozenset({"read", "write"})
permissions = {allowed: "editor"}

print("write" in allowed)
print(permissions[allowed])

Creates an immutable set of permissions and uses it as a dictionary key.

When to use it

Immutable membership collections

Representing a fixed, unordered collection of values

Composite dictionary keys

Using a collection of values as a dictionary key

Sets of sets

Nesting one set-like collection inside another

  • set
  • frozenset.union
  • frozenset.intersection
  • frozenset.difference
  • frozenset.symmetric_difference

Source: Built-in Types — Python 3.15.0 documentation. Checked 2026-10-15.