0% found this document useful (0 votes)
2 views5 pages

Frozenset in Python - Lokesh

The document provides an overview of the frozenset() method in Python, which creates an immutable set from an iterable, ensuring no duplicate values. It includes examples demonstrating its syntax, usage with different data types, and operations like union and intersection. Additionally, it highlights the immutability of frozensets and the errors that occur when attempting to modify them.

Uploaded by

vinku0419
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Frozenset in Python - Lokesh

The document provides an overview of the frozenset() method in Python, which creates an immutable set from an iterable, ensuring no duplicate values. It includes examples demonstrating its syntax, usage with different data types, and operations like union and intersection. Additionally, it highlights the immutability of frozensets and the errors that occur when attempting to modify them.

Uploaded by

vinku0419
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

11/12/24, 5:03 PM frozenset() in Python - GeeksforGeeks

rozenset() in Python
Last Updated : 03 Jul, 2024

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.

Python

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.

Python

https://www.geeksforgeeks.org/frozenset-in-python/ 1/5
11/12/24, 5:03 PM frozenset() in Python - GeeksforGeeks

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.

Python

# 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.

Python

https://www.geeksforgeeks.org/frozenset-in-python/ 2/5
11/12/24, 5:03 PM frozenset() in Python - GeeksforGeeks

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.

Python

# 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

Python

# creating a list
favourite_subject = ["OS", "DBMS", "Algo"]

https://www.geeksforgeeks.org/frozenset-in-python/ 3/5
11/12/24, 5:03 PM frozenset() in Python - GeeksforGeeks

# 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.

Python

# 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

https://www.geeksforgeeks.org/frozenset-in-python/ 4/5
11/12/24, 5:03 PM frozenset() in Python - GeeksforGeeks

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

https://www.geeksforgeeks.org/frozenset-in-python/ 5/5

You might also like