Python frozenset() Function
The frozenset()
function returns an immutable frozenset object, i.e. an immutable version of a Python Set.
Frozen sets can be used as keys in Dictionary or as elements of another set. But like Sets, a Frozen Set is not ordered (i.e. the elements can be set at any index).
Syntax
frozenset(iterable)
frozenset() Parameters
Python frozenset()
function parameters:
Parameter | Condition | Description |
---|---|---|
iterable | Optional | An iterable object which contains elements to initialize the frozenset with, like Set, Dictionary, Tuple, etc. |
frozenset() Return Value
Python frozenset()
function returns an immutable frozenset initialized with elements from the given iterable.
If no parameters are passed, it returns an empty frozenset.
Examples
Example 1: create a frozenset from a tuple with frozenset() function
# tuple of vowels
vowels = ('a', 'e', 'i', 'o', 'u')
fSet = frozenset(vowels)
print('The frozen set is:', fSet)
print('The empty frozen set is:', frozenset())
# Frozensets are immutable, so
# attempting to add to a frozenset raises an AttributeError
try:
fSet.add('v')
except AttributeError as e:
print(e)
output
The frozen set is: frozenset({'o', 'a', 'u', 'e', 'i'})
The empty frozen set is: frozenset()
'frozenset' object has no attribute 'add'
Example 2:
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
person = {"name": "Tom", "age": 25, "sex": "male"}
my_frozen_set = frozenset(person)
print('The frozen set is:', my_frozen_set)
output
The frozen set is: frozenset({'age', 'name', 'sex'})
Example 3: check membership in a frozenset
animals = frozenset(["cat", "dog", "lion"])
print("cat" in animals) # Output: True
print("elephant" in animals) # Output: False
output
True
False
Example 4: create a frozenset from a list
words = ["Tutorial", "Reference", "Tutorial", "site"]
fnum = frozenset(words)
print("frozenset Object is : ", fnum)
output
frozenset Object is : frozenset({'site', 'Reference', 'Tutorial'})
Example 5: create a frozenset for dictionary keys
# frozen set as a key in a dictionary
my_dict = {frozenset(['a', 'b']): 'value'}
print(my_dict[frozenset(['a', 'b'])]) # Output: 'value'
output
value
Example 6: error handling with frozensets
favourite_lang = ["Java", "C++", "Python"]
fset_subject = frozenset(favourite_lang)
# This line will raise an error because frozensets are immutable
try:
fset_subject[1] = "HTML"
except TypeError as e:
print(e)
output
TypeError: 'frozenset' object does not support item assignment
Frozenset Operations
Just as normal Sets, frozensets can also perform different operations like copy
, difference
, intersection
, symmetric_difference
, and union
.
# Initialize Frozensets A and B
A = frozenset([1, 2, 3, 4])
B = frozenset([3, 4, 5, 6])
# copying a frozenset
C = A.copy() # Output: frozenset({1, 2, 3, 4})
print(C)
# union
print(A.union(B)) # Output: frozenset({1, 2, 3, 4, 5, 6})
# intersection
print(A.intersection(B)) # Output: frozenset({3, 4})
# difference
print(A.difference(B)) # Output: frozenset({1, 2})
# symmetric_difference
print(A.symmetric_difference(B)) # Output: frozenset({1, 2, 5, 6})
output
frozenset({1, 2, 3, 4})
frozenset({1, 2, 3, 4, 5, 6})
frozenset({3, 4})
frozenset({1, 2})
frozenset({1, 2, 5, 6})
Similarly, other set methods like isdisjoint
, issubset
, and issuperset
are also available.
# initialize Frozensets A, B and C
A = frozenset([1, 2, 3, 4])
B = frozenset([3, 4, 5, 6])
C = frozenset([5, 6])
# isdisjoint() method
print(A.isdisjoint(C)) # Output: True
# issubset() method
print(C.issubset(B)) # Output: True
# issuperset() method
print(B.issuperset(C)) # Output: True
output
True
True
True