Python frozenset Function with Examples

The frozenset function in Python generates an immutable frozenset object that is initialized with elements sourced from the specified iterable.

Python frozenset Function Syntax

It has the following Syntax:

Example

frozenset(iterable)

Parameters

  • iterable: An object that can be iterated over, including but not limited to lists, tuples, and similar data structures.
  • Return

It produces a frozenset object that is immutable, created using the elements provided by the specified iterable.

Different Examples for Python frozenset Function

In this section, we will explore multiple examples demonstrating the use of the Python frozenset function.

Python frozenset Function Example 1

The following example demonstrates how the frozenset function operates in Python.

Example

# tuple of letters

letters = ('m', 'r', 'o', 't', 's')

fSet = frozenset(letters)

print('Frozen set is:', fSet)

print('Empty frozen set is:', frozenset())

Output:

Output

Frozen set is: frozenset({'o', 'm', 's', 'r', 't'})

Empty frozen set is: frozenset()

Explanation:

In the preceding illustration, we utilize a variable that is composed of a tuple containing letters, and it yields an immutable frozenset object.

Python frozenset Function Example 2

The following example demonstrates how frozenset operates in conjunction with dictionaries.

Example

# random dictionary

person = {"name": "Phill", "age": 22, "sex": "male"}

fSet = frozenset(person)

print('Frozen set is:', fSet)

Output:

Output

Frozen set is: frozenset({'name', 'sex', 'age'})

Input Required

This code uses input(). Please provide values below: