Friday, November 15, 2024
Google search engine
HomeLanguagesfrozenset() in Python

frozenset() in Python

Python Method creates an immutable Set object from an iterable. It is a built-in Python function. As it is a set object, therefore, we cannot have duplicate values in the frozenset.

Example

In this example, we are creating a frozen set with the list in Python.

Python3




animals = frozenset(["cat", "dog", "lion"])
print("cat" in animals)
print("elephant" in animals)


Output

True
False

Python frozenset() Syntax 

Syntax : frozenset(iterable_object_name)
Parameter : iterable_object_name

  • This function accepts iterable object as input parameter.

Return :  Returns an equivalent frozenset object.

frozenset() in Python Examples

Working of Python frozenset()

In this example, we are showing how to create a frozen set in Python and we are showing that frozen set objects are immutable and can not be modified after the creation.

Python3




fruits = frozenset(["apple", "banana", "orange"])
print(fruits)
fruits.append("pink")
print(fruits)


Output

frozenset({'banana', 'orange', 'apple'})
Traceback (most recent call last):
File "C:\Users\ashub\OneDrive\Desktop\achal cahtgpt\temp.py", line 3, in <module>
fruits.append("pink")
AttributeError: 'frozenset' object has no attribute 'appen

Python frozenset for Tuple

If no parameters are passed to frozenset() function, then it returns an empty frozenset type object in Python.  

Python3




# passing an empty tuple
nu = ()
 
# converting tuple to frozenset
fnum = frozenset(nu)
 
# printing empty frozenset object
print("frozenset Object is : ", fnum)


Output

frozenset Object is :  frozenset()

Python frozenset for List

Here as a parameter a list is passed and now its frozen set object is returned.

Python3




l = ["Geeks", "for", "Geeks"]
  
# converting list to frozenset
fnum = frozenset(l)
  
# printing empty frozenset object
print("frozenset Object is : ", fnum)


Output 

frozenset Object is :  frozenset({'Geeks', 'for'})

frozenset in Python for Dictionary

Since frozen set objects are immutable, they are mainly used as keys in dictionary or elements of other sets. The below example explains it clearly. 

Python3




# creating a dictionary
Student = {"name": "Ankit", "age": 21, "sex": "Male",
           "college": "MNNIT Allahabad", "address": "Allahabad"}
 
# making keys of dictionary as frozenset
key = frozenset(Student)
 
# printing dict keys as frozenset
print('The frozen set is:', key)


Output

The frozen set is: frozenset({'address', 'name', 'age', 'sex', 'college'})

Exceptions while using the frozenset() method in Python

If by mistake we want to change the frozen set object, then it throws a TypeError

Python3




# creating a list
favourite_subject = ["OS", "DBMS", "Algo"]
 
# creating a frozenset
f_subject = frozenset(favourite_subject)
 
# below line will generate error
f_subject[1] = "Networking"


Output 

TypeError                                 Traceback (most recent call last)
Input In [13], in <cell line: 8>()
5 f_subject = frozenset(favourite_subject)
7 # below line will generate error
----> 8 f_subject[1] = "Networking"
TypeError: 'frozenset' object does not support item assignment

Frozenset operations

Frozen sets are immutable sets that allow you to perform various set operations such as union, intersection, difference, and symmetric difference.

Python3




# initialize A and B
A = frozenset([1, 2, 3, 4])
B = frozenset([3, 4, 5, 6])
 
# copying a frozenset
C = A.copy()
print(C) 
 
# union
union_set = A.union(B)
print(union_set)
 
# intersection
intersection_set = A.intersection(B)
print(intersection_set) 
 
difference_set = A.difference(B)
print(difference_set)
 
# symmetric_difference
symmetric_difference_set = A.symmetric_difference(B)
print(symmetric_difference_set)


Output 

frozenset({1, 2, 3, 4})
frozenset({1, 2, 3, 4, 5, 6})
frozenset({3, 4})
frozenset({1, 2})
frozenset({1, 2, 5, 6})

Previous article
Next article
RELATED ARTICLES

Most Popular

Recent Comments