Skip to main content

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
Submitted by devanshi.srivastava on June 25, 2021

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.