Visitar URL original
frozenset | Python’s Built-in Data Types – Real Python
Skip to content

frozenset

The built-in frozenset data type is similar to a set, but it’s immutable. This means that once a frozenset is created, its elements can’t be changed, added, or removed.

Frozensets are useful when you need a set that should remain constant throughout the program’s execution:

Language: Python
>>> frozenset(['apple', 'banana', 'cherry'])
frozenset({'apple', 'banana', 'cherry'})

frozenset Constructor

Language: Python Syntax
frozenset(iterable=(), /)

Arguments

Argument Description Default Value
iterable An iterable object whose elements will populate the frozenset. Empty tuple, ()

Return Value

  • Returns a Python frozenset object

frozenset Examples

Creating an empty frozenset by calling frozenset() with no arguments:

Language: Python
>>> empty_fs = frozenset()
>>> empty_fs
frozenset()

Creating a frozenset using a list as an argument:

Language: Python
>>> frozenset(["apple", "banana", "cherry"])
frozenset({'apple', 'banana', 'cherry'})

frozenset Methods

Method Description
.copy() Returns a shallow copy of the frozenset.
.difference(*others) Returns a new frozenset with elements in self that are not in the others.
.intersection(*others) Returns a new frozenset with elements common to self and all others.
.isdisjoint(other) Returns True if self and other have no elements in common.
.issubset(other) Returns True if all elements of self are in other.
.issuperset(other) Returns True if all elements of other are in self.
.symmetric_difference(other) Returns a new frozenset with elements in either self or other, but not both.
.union(*others) Returns a new frozenset with elements from self and all others.

frozenset Common Use Cases

The most common use cases for frozenset include:

  • Using it as a key in a dictionary
  • Storing sets of data that shouldn’t be changed
  • Performing set operations without modifying the original set

frozenset Real-World Example

Say that you need to define an immutable set of constants to represent valid colors in a drawing app:

Language: Python
>>> VALID_COLORS = frozenset(["red", "green", "blue"])
>>> "red" in VALID_COLORS
True
>>> "purple" in VALID_COLORS
False

In this example, you define the VALID_COLORS constant using a frozenset. Then, you use the constant in membership tests because sets are highly efficient for this type of operation.

Common Python Data Structures (Guide)

Course

Records and Sets: Selecting the Ideal Data Structure

In this course, you'll learn about two of Python's data structures: records and sets. You'll look at multiple types and classes for both of these and learn which implementations are best for your specific use cases.

basics data-structures python

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Leodanis Pozo Ramos • Updated Oct. 11, 2026 • Reviewed by Brenda Weleschuk and Dan Bader