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:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn Python on Codecademy