Python frozenset()

BrandonDusch's avatar
Published May 23, 2022
Contribute to Docs

The built-in frozenset() function returns a new frozenset using an optional iterable object such as a string or list.

Frozensets are a specific type of set that are immutable. Like tuples, their contents are frozen and cannot be changed after creation.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

frozenset(iterable)

The iterable parameter is optional and must be a hashable object. If nothing is passed into the frozenset() function, an empty frozenset is created.

Example

In the following example, a frozenset of frozen_objects is created:

frozen_objects = frozenset(['ice cube', 'glacier', 'frozen_set'])
print(frozen_objects)
print(type(frozen_objects))

The following output will look like this:

frozenset({'glacier', 'ice cube', 'frozen_set'})
<class 'frozenset'>

Codebyte Example

In the example below, the frozenset() function is used to create a new frozenset called continents:

Code
Output

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours