Python Frozenset With Examples

Introduction to Python Frozenset

The Python frozenset() function creates an immutable frozenset object from the specified iterable’s elements. An immutable version of a Python set object is a frozen set. While parts of a set can be changed at any moment, elements of a frozen set don’t change after they’ve been created. As a result, frozen sets can be use as Dictionary keys or as elements of other sets. It is not, however, organized in the same way that sets are (the elements can be set at any index).

Syntax:

frozenset(iterable_object_name)

Parameters: This function accepts an iterable object as an input parameter.

  • iterable (Optional): the iterable which contains elements to initialize the frozenset with. Iterable can be set, dictionary, tuple, etc.

Return: The frozenset() function returns an immutable frozenset initialized with elements from the given iterable. If no parameters are pass, it returns an empty frozenset.

Code Examples of Python Frozenset

Example 01:

# tuple of vowels
numbers = ('1', '2', '3', '4', '5')
set = frozenset(numbers)
print('The frozen set is:', set)
print('The empty frozen set is:', frozenset())

# frozensets are immutable
set.add('6')

Output:

The frozen set is: frozenset({'4', '5', '1', '3', '2'})
The empty frozen set is: frozenset()
Traceback (most recent call last):
File "<string>", line 8, in <module>
AttributeError: 'frozenset' object has no attribute 'add'

Example 02: Frozenset() for Dictionary – When you use a dictionary as an iterable for a frozen set, it only takes keys of the dictionary to create the set.

# random dictionary
animal = {"Dog": 20, "Cat": 23, "Leopard": 2, "Tiger":1}

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

Output:

The frozen set is: frozenset({'Cat', 'Tiger', 'Dog', 'Leopard'})

Example 03: Frozenset Operations

  • copy
  • difference
  • intersection
  • symmetric_difference
  • union
  • isdisjoint
  • issubset
  • issuperset
# Frozensets
# initialize A , B
 and C
A = frozenset([4, 3, 2, 1])
B = frozenset([7, 6, 5, 4])
C = frozenset([5, 4])

# copy
D = A.copy()
print("Copy of frozen set: ", D)

# union
print("Union of frozen set: ", A.union(B)) 

# intersection
print("Intersection of frozen set: ", A.intersection(B)) 

# difference
print("Difference of frozen set: ", A.difference(B))  

# symmetric_difference
print("symmetric_difference of frozen set: ", A.symmetric_difference(B))  

# isdisjoint() method
print("disjoint of frozen set: ", A.isdisjoint(C))

# issubset() method
print("subset of frozen set: ", C.issubset(B))

Output:

Copy of frozen set:  frozenset({1, 2, 3, 4})
Union of frozen set:  frozenset({1, 2, 3, 4, 5, 6, 7})
Intersection of frozen set:  frozenset({4})
Difference of frozen set:  frozenset({1, 2, 3})
symmetric_difference of frozen set:  frozenset({1, 2, 3, 5, 6, 7})
disjoint of frozen set:  False
subset of frozen set:  True
Superset of frozen set:  True

FAQs

What is the difference between set and Frozenset?

Set objects may be create with curly braces and literal. Set objects are changeable and unhashable, whereas frozenset objects are immutable and hashable, allowing them to be use as elements of other set objects and dictionaries’ keys.

The difference between tuples and frozensets in Python

  • Tuples are immutable lists, frozensets are immutable sets.
  • Tuples are indeed an ordered collection of objects, but they can contain duplicates and unhashable objects, and have slice functionality
  • frozensets aren’t index, but you have the functionality of sets. Element lookups, and functionality such as unions and intersections. They also can’t contain duplicates, like their mutable counterparts.

Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn

Suggested Articles:

  1. Python Tuple
  2. Python List

Leave a Comment