Skip to content

Python Frozen Set




Python – Frozen Set


As we read earlier about Python Set , Now we will study about Frozen Set in python. In python programming language, this is our last collection of data type. In this, we will cover what is frozen set, access the frozenset items, frozen set methods etc.  so let’s start study about the last collection data type.





What is Frozen Set ?

frozenset is a collection heterogeneous data type. It is an unordered collection of elements. It is a immutable data type i.e. they cannot be modified or edited.



Python frozenset syntax :

frozenset( iterable )

# iterable object like list, tuple, set etc.



Python frozenset example :

set = {2, 5, "abc"}
fs = frozenset(set)
print(fs)



Output :

frozenset({'abc', 2, 5})




How to check the data type of a frozenset in python ?

To check the data type of a frozenset, you have to use the type( ) function.



Example :

set = {2, 5, "abc"}
fs = frozenset(set)
print(type(fs))



Output :

<class 'frozenset'>




How to access the frozenset items from a frozenset in python ?

In frozenset, we cannot access the elements of frozenset by using the index number because frozensets are unordered, the frozenset items has no index position.





Frozenset Methods in python


1. symmetric_difference( ) :

 It returns a frozenset with the symmetric difference of two frozensets.



General syntax for symmetric_difference( ) :

frozenset.symmetric_difference(iterable) 



Example :

fs_1 = frozenset({1, 2, 3, 4, 5})
fs_2 = frozenset({6, 7, 5, 2, 1})
fs_3 = fs_1.symmetric_difference(fs_2)
print(fs_3)



Output :

frozenset({3, 4, 6, 7})

It returns a frozenset contains a mix of items that are not present in both the frozensets.





2. issuperset( ) : 

It always returns whether this frozenset contains another frozenset or not.



General syntax for issuperset( ) :

frozenset.issuperset( iterable )



Example :

fs_1 = frozenset({1, 2, 3, 4, 5})
fs_2 = frozenset({6, 7, 5, 2, 1})
fs_3 = fs_1.issuperset(fs_2)
print(fs_3)



Output :

False

It returns True if all the items in this specified frozenset exists in the original frozenset, otherwise return False





3. issubset( ) : 

It returns whether another frozensets contains this set or not.



General syntax for issubset( ) :

frozenset.issubset( iterable )



Example :

fs_1 = frozenset({1, 2, 3})
fs_2 = frozenset({6, 3, 1, 2})
fs_3 = fs_1.issubset(fs_2)
print(fs_3)



Output :

True

It returns True if all the items are present in the specified frozenset, otherwise returns False.





4. isdisjoint( ) :

It returns whether two frozensets have a intersection or not.



General syntax for isdisjoint( ) :

frozenset.isdisjoint( iterable ) 



Example :

fs_1 = frozenset({1, 2, 3, 4})
fs_2 = frozenset({7, 8, 9})
fs_3 = fs_1.isdisjoint(fs_2)
print(fs_3)



Output :

True

It returns True if none of the items are present in both frozensets, otherwise returns False if items are not present in both frozensets.





5. intersection( ) :

It returns a frozenset, i.e the intersection of two other frozensets.



General syntax for intersection( ) :

frozenset.intersection( iterable )



Example :

fs_1 = frozenset({1, 2, 3, 4})
fs_2 = frozenset({7, 8, 9})
fs_1.intersection(fs_2)
print(fs_1)



Output :

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




6. difference( ) : 

It returns a frozenset containing the difference between two or more frozensets.



General syntax for difference( ) :

frozenset.difference( iterable )



Example :

fs_1 = frozenset({1, 2, 3, 4})
fs_2 = frozenset({6, 2, 4})
fs_3 = fs_1.difference(fs_2)
print(fs_3)



Output :

frozenset({1, 3})




7. union( ) :

It returns a new frozenset containing all items from both frozensets.



General syntax for union( ) :

fronzenset.union( iterable )



Example :

fs_1 = frozenset({1, 2, 3, 4})
fs_2 = frozenset({6, 2, 4})
fs_3 = fs_1.union(fs_2)
print(fs_3)



Output :

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



>