Python frozenset() with Examples

Python frozenset is an unordered collection of distinct hashable objects. Frozenset is an immutable set, so its contents cannot be modified after it’s created.

Python frozenset()

Python frozenset() function is used to create frozenset object. Its syntax is:

If the input iterable argument is provided, then forzenset is created from the iterable elements. If no argument is provided, then an empty frozenset object is returned.

Python frozenset() example

Let’s see how to use frozenset() function to create frozenset object.


Iterating frozenset elements

We can use for loop to iterate through frozen set elements.


Note that the duplicate elements are removed while frozenset is being created.

Python frozenset functions

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

Let’s look at some of the methods available for the frozenset object.

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


Python frozenset to list, tuple

We can easily create list and tuple from frozenset object using built-in functions.


That’s all for python frozenset object and frozenset() built-in function.

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation

By admin

Leave a Reply

%d bloggers like this: