Python frozenset()
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.
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:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve 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