Unit 1 CHP 2
Unit 1 CHP 2
1. Frozen set is just an immutable version of a Python set object. While elements of a set
can be modified at any time, elements of the frozen set remain the same after creation.
2. Due to this, frozen sets can be used as keys in Dictionary or as elements of another set.
3. But like sets, it is not ordered (the elements can be set at any index).
4. The syntax of frozenset() function is:
frozenset([iterable])
Iterable can be set, dictionary, tuple, etc.
5. Return value from frozenset(): The frozenset() function returns an immutable frozenset
initialized with elements from the given iterable. If no parameters are passed, it returns
an empty frozenset.
6. Example:
a) Creating frozenset using tuple
# tuple of vowels
vowels = ('a', 'e', 'i', 'o', 'u')
fSet = frozenset(vowels)
print('The frozen set is:', fSet)
c) 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.
2. Example:
A = frozenset([1, 2, 3, 4])
B = frozenset([3, 4, 5, 6])
# copying a frozenset
C = A.copy()
print(C)
# union
print(A.union(B))
# intersection
print(A.intersection(B))
# difference
print(A.difference(B))
# symmetric_difference
print(A.symmetric_difference(B))