The frozenset()
function in Python creates an immutable set. It takes an iterable object as an optional parameter and returns a frozenset object with the elements of the iterable. Once a frozenset object is created, its elements cannot be modified, added, or removed.
Parameter Values
Parameter | Description |
---|---|
iterable | An iterable (list, tuple, dict, etc.) whose elements will be used to create the frozenset. |
Return Values
The frozenset()
function returns an immutable frozenset
object.
How to Use frozenset()
in Python
Example 1:
Return a new frozenset
, a set that is immutable.
frozen_set = frozenset([1, 2, 3, 4, 5])
Example 2:
Create a frozenset
from a list of strings.
string_set = frozenset(['apple', 'banana', 'cherry'])
Example 3:
Generate a frozenset
from a tuple of tuples.
tuple_set = frozenset(((1, 2), (3, 4), (5, 6))