Python frozenset() Function

Returns an immutable (unchangeable) set. This is useful for when you want to prevent the set from being changed. In the following example, we see that an exception is thrown when trying to add another item to the frozenset object.

Syntax

Python
frozenset()
frozenset(iterable)

Parameters

ParameterDescription
iterable Optional. An iterable object to convert to an immutable collection.

Example

Python
elements = {'earth', 'wind', 'fire', 'air'}
frozenSet = frozenset(elements)
frozenSet.add('the 5th element')

Output

Traceback (most recent call last):
  File "frozenSetTest.py", line 3, in <module>
    frozenSet.add('the 5th element')
AttributeError: 'frozenset' object has no attribute 'add'