Skip to content

Python frozenset() Function

How to create immutable frozensets in Python using frozenset(), including syntax, parameters, examples with lists/tuples/dictionaries, and key operations like union, intersection, and membership checks.

The frozenset() function returns an immutable frozenset object, i.e. an immutable version of a Python Set.

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.

Examples

Example 1: create a frozenset from a tuple with frozenset() function

vowels = ('a', 'e', 'i', 'o', 'u')

fSet = frozenset(vowels)
print('The frozen set is:', fSet)
print('The empty frozen set is:', frozenset())

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.

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

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)

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.

A = frozenset([1, 2, 3, 4])
B = frozenset([3, 4, 5, 6])

C = A.copy()  # Output: frozenset({1, 2, 3, 4})
print(C)

print(A.union(B))  # Output: frozenset({1, 2, 3, 4, 5, 6})

print(A.intersection(B))  # Output: frozenset({3, 4})

print(A.difference(B))  # Output: frozenset({1, 2})

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.

A = frozenset([1, 2, 3, 4])
B = frozenset([3, 4, 5, 6])
C = frozenset([5, 6])

print(A.isdisjoint(C))  # Output: True

print(C.issubset(B))  # Output: True

print(B.issuperset(C))  # Output: True

output

True
True
True
Navigation