AppDividend
Latest Code Tutorials

Python Frozenset Example | frozenset() in Python Tutorial

0

Python Frozenset Example | frozenset() in Python Tutorial is today’s topic. The frozenset() is an inbuilt function in Python which takes an iterable object as input and makes them immutable. In Python, frozenset is same as set except its items are immutable. This function takes input as an iterable object and converts them into an immutable object. The order of item is not guaranteed to be preserved.

Python Frozenset Example

The frozenset() method returns an immutable frozenset object initialized with elements from the given iterable. Python frozenset() method helps us to convert the mutable object to an immutable object. The frozen set is just an immutable version of a Python set object. 

See the following syntax of frozenset() function.

frozenset(iterable)

The frozenset() function accepts the iterable object as an input parameter. Iterable can be set, dictionarytuple, etc.

See the following code example.

# app.py

cast = ('Millie', 'Winona', 'David', 'Finn', 'Gaten', 'Caleb', 'Noah)

frozencast = frozenset(cast)
print('The frozen set is:', frozencast)

See the following output.

➜ pyt python3 app.py
The frozen set is: frozenset({'Finn', 'Caleb', 'David', 'Millie', 'Winona', 'Gaten', 'Noah})
➜ pyt

Let’s take an example of Python Dictionary.

# app.py

castD = {
  'name': 'Krunal',
  'age': 26,
  'education': 'BE'
}

frozencast = frozenset(castD)
print('The frozen set is:', frozencast)

See the output.

➜  pyt python3 app.py
The frozen set is: frozenset({'education', 'name', 'age'})
➜  pyt

Like normal sets, frozenset can also perform different operations like union, intersection, etc.

#Modify Frozenset

Okay, let’s try to modify the Frozenset and see the following output.

# app.py

cast = ['Millie', 'Winona', 'David', 'Finn', 'Gaten', 'Caleb']

frozencast = frozenset(cast)
frozencast[1] = 'Sadie'

See the following output.

➜  pyt python3 app.py
Traceback (most recent call last):
  File "app.py", line 4, in <module>
    frozencast[1] = 'Sadie'
TypeError: 'frozenset' object does not support item assignment
➜  pyt

#Iterating frozenset elements

We can use for loop to iterate through frozen set elements. See the following code.

# app.py

cast = ['Millie', 'Winona', 'David', 'Finn', 'Gaten', 'Caleb']

frozencast = frozenset(cast)
for data in frozencast:
    print(data)

See the following output.

➜  pyt python3 app.py
Gaten
Finn
Millie
Winona
Caleb
David
➜  pyt

#Python frozenset functions

The frozenset is immutable, there are no methods available to modify its items. So add(), update(), remove(), pop() etc. functions are not defined for frozenset.

Still, some of the functions available for the frozenset object, which are the following.

  1. len(fs): It returns the number of elements in the frozenset.
  2. x in fs: It returns True if x is present in fs, else returns False.
  3. x not in fs: It returns True if x is not present in fs, else returns False.
  4. isdisjoint(other): It returns True if the frozenset has no elements in common with others. Two sets are disjoint if and only if their intersection is the empty set.
  5. issubset(other): It returns True if every element of the set is present in the other set, else returns False.
  6. issuperset(other): It returns True if every element in other is present in the set, else returns False.
  7. union(*others): It returns a new frozenset object with elements from the frozenset and other sets.
  8. intersection(*others): It returns a new frozenset with elements from this set and all the other sets.
  9. difference(*others): It returns a new frozenset with elements in the frozenset that are not in the other sets.
  10. symmetric_difference(other): It returns a new frozenset with elements in either the frozenset or other but not both.

Finally, Python Frozenset Example | frozenset() in Python Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.