





Please login

Prepinsta Prime
Video courses for company/skill based Preparation

Prepinsta Prime
Purchase mock tests for company/skill building
Python frozenset()
frozenset() in Python
In Python, frozenset is same as set except its elements are immutable. frozenset() is an inbuilt function is Python.
This function takes any iterable object as input parameter and converts them into immutable object.Simply it freezes the iterable objects and makes them unchangeable. Order of element may change.

Example :
frozenset() method freeze the list, and make it unchangeable:
# list of numbers
numList = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
# converting list to frozenset
fnum = frozenset(numList)
# fnum will contain
fnum=( { 1, 2, 3, 4, 5, 6, 7, 8, 9 } )
Syntax :
Return Type :
The frozenset() function returns an unchangeable frozenset object (which is like a set object, only unchangeable).
Output : frozenset({1, 2, 3, 4, 5}) frozenset({8, 9, 7, 6, 0}) frozenset({1, 2, 3, 4, 5}) frozenset({'Muskan', 'Vaibhav', 'Ankita', 'Simran', 'Komal'})
If we try to modify this frozenset() then error will occur.
Output : TypeError Traceback (most recent call last) 7 #modifying the frozenset() ----> 8 fList[1] = 20 9 print(fList) TypeError: 'frozenset' object does not support item assignment
Login/Signup to comment