Python frozenset()

Python frozenset() builtin function creates and returns a new frozenset object from given iterable object.

In this tutorial, we will learn about the syntax of Python frozenset() function, and learn how to use this function with the help of examples.

Syntax

The syntax of frozenset() function is

frozenset([iterable])

where

ParameterRequired/OptionalDescription
iterableOptionalAn iterable object like list, set, tuple, etc., or any object that implements __iter__() method.

If we pass a list/tuple/set to frozenset(), then it returns a frozen set created with the elements from the given list/tuple/set.

If we do not pass an argument to frozenset(), then it returns an empty frozen set.

Returns

The function returns object of type frozenset.

Example – frozenset()

In this example, we will create a frozen set from a Python List object.

Python Program

iterable = [2, 4, 6, 8]
result = frozenset(iterable)
print(f'Return value : {result}')
Try Online

Output

Return value : frozenset({8, 2, 4, 6})

frozenset() from Set

In the following program, we will create a frozen set from a Python Set object.

Python Program

iterable = {2, 4, 6, 8}
result = frozenset(iterable)
print(f'Return value : {result}')
Try Online

Output

Return value : frozenset({8, 2, 4, 6})

frozenset() from Tuple

In the following program, we will create a frozen set from a Python Tuple object.

Python Program

iterable = (2, 4, 6, 8)
result = frozenset(iterable)
print(f'Return value : {result}')
Try Online

Output

Return value : frozenset({8, 2, 4, 6})

Empty frozenset

In the following program, we will create an empty frozen set using frozenset() function. Pass no argument to frozenset() function, and the function returns an empty frozenset object.

Python Program

iterable = (2, 4, 6, 8)
result = frozenset(iterable)
print(f'Return value : {result}')
Try Online

Output

Return value : frozenset()

Conclusion

In this Python Tutorial, we have learnt the syntax of Python frozenset() builtin function, and also learned how to use this function, with the help of Python example programs.