Programs

Everything You Need To Know About Frozenset In Python

Python is a language that holds the key features of both functional as well as object-oriented programming languages. Unlike tuples or lists, the sets in Python are basically an unranked assembly of unique items. The sets in Python are characterized by comma-separated values, put inside curly braces or {}. However, all the elements in these sets are unordered. 

In this article, we are going to look at the data type frozenset in Python.

WHAT DOES FROZENSET MEAN?

Frozenset is defined as a fixed or inflexible unorganized group of elements that are unique in nature. These sets do hold a group of elements, but there is no order of these elements inside the set. Going by the name implies that no changes can be made to the elements of the frozenset once it has been created. 

Like tuples, frozensets are also immutable in nature. Holding the property of being hashable, they can also be used as keys in a dictionary. This type of sets supports all kinds of functionality like copy(), union(), isdisjoint(), issuperset(), symmetric_difference(), intersection() etc.

Explore our Popular Data Science Courses

  • frozenset() is the function used to create such types of sets. For eg,

                              frozenset([11,12,13])

                             frozenset ({11,12,13})

  • Following is an example that illustrates the hashable property of the frozen sets:

               { {11}, {12,13} }

               TypeError: unhashable type: ‘set’

               {frozenset ( [11] ), frozenset ( [12,13] ) }

                {frozenset ( {11} ), forzenset ( {12,13} ) }

  • Frozenset()- This function takes a single parameter.
  • The return type of the frozenset() function is a fixed frozenset that starts with the elements which have been passed in the parameter.
  • In case no parameters have been passed in the set, the output of the function frozenset() will be a null frozenset. 

Read our popular Data Science Articles

Check Out upGrad’s Data Science Courses 

OPERATIONS ON FROZENSET

In this section, we are going to understand the different types of operations performed on Frozensets with the help of examples.

  • Union()

The Union() function is used to find the union of any two frozensets (A1 & A2), which returns all the elements that are present in both sets.

Eg.

#frozensets

#intialize A1 and A2

A1= frozenset ( (a, b, c, d) )

A2= frozenset ( (e, b, d, h, i) )

Output= A1.union(A2)

Print(output)

# OUTPUT: frozenset ( {b, d} )

Explore our Popular Data Science Courses

  • Copy ()

Copy() function allows the user to copy all the elements of frozenset A1 into another set A3.

Eg.

#frozensets

#intialize A1 & A2

A1= frozenset ( [a, b, c, d] )

A2= frozenset ( [e, b, d, h, i] )

#copying a frozenset

A3 = A1.copy()

Print (A3)

#OUTPUT: frozenset( {a,b,c,d} )

Read our popular Data Science Articles

  • Symmentric_difference()

This function returns all the elements present in one of the many sets exactly. 

Eg.

#frozensets

#intialize A1 & A2

A1 = frozenset ( [b, c, d] )

A2 = frozenset ( [e, b, c, d, i] )

Output = A1.symmetric_difference(A2)

Print(output)

#OUTPUT: frozenset( {e, i} )

  • Issubset()

This function allows the user to check if any set A1 is the subset of another set A2. It will return the value ‘True’ or ‘False’.

#frozensets

#intialize A1 & A2

A1 = frozenset ( [b, c, d] )

A2 = frozenset ( [e, b, c, d, i] )

Output = A1.issubset(A2)

Print(output)

#OUTPUT: True

A1 = frozenset ( [a,b, c, d] )

A2 = frozenset ( [e, b, c, d, i] )

Output = A1.issubset(A2)

Print(output)

#OUTPUT: False

CONCLUSION 

Although both sets and frozensets are capable of storing unique elements, they can very easily be implemented in iteration. In contrast to the normal sets, elements of frozenset in python are hashable as well as immutable and can therefore be used as keys for dictionaries and objects for other sets.

If you are curious to learn about tableau, data science, check out IIIT-B & upGrad’s Executive PG Programme in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.

Want to share this article?

Prepare for a Career of the Future

Leave a comment

Your email address will not be published. Required fields are marked *

Leave a comment

Your email address will not be published. Required fields are marked *

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks