Python: FrozenSets

Frozenset() is an in-built function in Python. It helps in converting a mutable list to an immutable one. It freezes the iterable objects and makes them unchangeable.

This function simply takes input as any iterable object and converts them into an immutable object.

Syntax:

frozenset(iterable)

Parameter:

  •  iterable: This function takes iterable object like list, set, tuple as an input parameter.

Return:

  • It returns an immutable frozenset initialized with elements from the given iterable and, if no parameters are passed, it returns an empty frozenset.

Example:

tup_1 = ('a','b','c','d')
tup_2 = frozenset(tup_1)
print('The frozen set is:', tup_2)
tup_2.add('e')

Output:

The frozen set is: frozenset({'c', 'a', 'd', 'b'})
---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)
<ipython-input-1-106d6402896c> in <module>
      2 tup_2=frozenset(tup_1)
      3 print('The frozen set is:', tup_2)
----> 4 tup_2.add('e')


AttributeError: 'frozenset' object has no attribute 'add'
Tags