Python frozenset()

Filed Under: Python

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:


class frozenset([iterable])

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.


fs = frozenset()
print(fs)
print(type(fs))

# frozenset from iterable
list_vowels = ['A', 'E', 'I', 'O', 'U']
fs = frozenset(list_vowels)
print(fs)

tuple_numbers = (1, 2, 3, 4, 5, 4, 3)
fs = frozenset(tuple_numbers)
print(fs)

Output:


frozenset()
<class 'frozenset'>
frozenset({'E', 'U', 'I', 'O', 'A'})
frozenset({1, 2, 3, 4, 5})

Iterating frozenset elements

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


fs = frozenset([1, 2, 3, 4, 5, 4, 3])
for x in fs:
    print(x)

Output:


1
2
3
4
5

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.

fs = frozenset([1, 2, 3, 4, 5])

size = len(fs)
print('frozenset size =', size)

contains_item = 5 in fs
print('fs contains 5 =', contains_item)

not_contains_item = 6 not in fs
print('fs not contains 6 =', not_contains_item)

is_disjoint = fs.isdisjoint(frozenset([1, 2]))
print(is_disjoint)

is_disjoint = fs.isdisjoint(frozenset([10, 20]))
print(is_disjoint)

is_subset = fs.issubset(set([1, 2]))
print(is_subset)

is_subset = fs.issubset(set([1, 2, 3, 4, 5, 6, 7]))
print(is_subset)

is_superset = fs.issuperset(frozenset([1, 2]))
print(is_superset)

is_superset = fs.issuperset(frozenset([1, 2, 10]))
print(is_superset)

fs1 = fs.union(frozenset([1, 2, 10]), set([99, 98]))
print(fs1)

fs1 = fs.intersection(set((1, 2, 10, 20)))
print(fs1)

fs1 = fs.difference(frozenset([1, 2, 3]))
print(fs1)

fs1 = fs.symmetric_difference(frozenset([1, 2, 10, 20]))
print(fs1)

fs1 = fs.copy()
print(fs1)

Output:


frozenset size = 5
fs contains 5 = True
fs not contains 6 = True
False
True
False
True
True
False
frozenset({1, 2, 3, 4, 5, 98, 99, 10})
frozenset({1, 2})
frozenset({4, 5})
frozenset({3, 20, 4, 5, 10})
frozenset({1, 2, 3, 4, 5})

Python frozenset to list, tuple

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


fs = frozenset([1, 2, 3, 4, 5])
l1 = list(fs)
print(l1)

t1 = tuple(fs)
print(t1)

Output:


[1, 2, 3, 4, 5]
(1, 2, 3, 4, 5)

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

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages