frozenset() Built-in Function

Python – frozenset()

Python frozenset() built-in function is used to create a frozenset object from the elements of an optional iterable.

A frozenset is immutable unlike a regular set. Once, the frozenset is created, we cannot add, update, or delete elements of it. We can only read them. The elements in a frozenset must also be immutable.

In this tutorial, we will learn the syntax and usage of frozenset() built-in function with examples.

Syntax

The syntax of frozenset() function is

frozenset(iterable=set())
ParameterDescription
iterableAn iterable object.

Examples

1. Create a frozenset from a set of integers

In the following program, we create a frozenset object from a set of elements.

Python Program

mySet = {2, 10, 8, 4, 6}
myFrozenset = frozenset(mySet)
print(myFrozenset)
Run Code Copy

Output

frozenset({2, 4, 6, 8, 10})

Summary

In this Built-in Functions tutorial, we learned the syntax of the frozenset() built-in function, and how to use this function to create a frozenset object.

Related Tutorials

Code copied to clipboard successfully 👍