Python frozenset()
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 CoursesIncludes 6 Courses
- With Professional CertificationWith Professional Certification
- Beginner Friendly.75 hours75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With CertificateWith Certificate
- Beginner Friendly.24 hours24 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))Copy to clipboardCopy to clipboard
The following output will look like this:
frozenset({'glacier', 'ice cube', 'frozen_set'})<class 'frozenset'>Copy to clipboardCopy to clipboard
Codebyte Example
In the example below, the frozenset() function is used to create a new frozenset called continents:
Visit usVisit usCodeHide codeOutputHide outputCopy to your clipboard
All contributors
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 CoursesIncludes 6 Courses
- With Professional CertificationWith Professional Certification
- Beginner Friendly.75 hours75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With CertificateWith Certificate
- Beginner Friendly.24 hours24 hours
