Skip to main content

4.6 - Working With Frozensets

A frozenset is a new class that has the characteristics of a set, but its elements cannot be changed once assigned. It’s defined by using the frozenset() function. Like sets, frozensets are also collections of unique elements.

Frozensets are immutable, meaning their elements cannot be modified after creation. They are useful for scenarios where you need a set that cannot change, such as a key in a dictionary.

4.6.1 - Creating Frozensets

Frozensets are created using the frozenset() function:

# Creating an empty frozenset
empty_frozenset = frozenset()

# Creating a frozenset from a list
fs_from_list = frozenset([1, 2, 3])

# Creating a frozenset from a string
fs_from_string = frozenset("hello")

4.6.2 - Immutability of Frozensets

Unlike sets, frozensets are immutable, meaning their elements cannot be changed once created:

# Attempting to add an element to a frozenset will raise an error
# fs_from_list.add(4) # This would raise an AttributeError

4.6.3 - Set Operations with Frozensets

Frozensets support various set operations:

  • Union: frozenset1 | frozenset2
  • Intersection: frozenset1 & frozenset2
  • Difference: frozenset1 - frozenset2
  • Symmetric Difference: frozenset1 ^ frozenset2

Example:

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

union = a | b # frozenset([1, 2, 3, 4, 5])
intersection = a & b # frozenset([3])
difference = a - b # frozenset([1, 2])
symmetric_difference = a ^ b # frozenset([1, 2, 4, 5])

4.6.4 - Checking Membership

To check if an item is a member of a frozenset, use the in keyword:

if 1 in fs_from_list:
print("1 is in the frozenset")

4.6.5 - Comparing Frozensets

Frozensets can be compared using operators like ==, !=, <, <=, > and >=:

fs1 = frozenset([1, 2])
fs2 = frozenset([1, 2, 3])

print(fs1 == fs2) # False
print(fs1 < fs2) # True

4.6.6 - Iterating Over Frozensets

Iterate over the elements of a frozenset using a for loop:

for element in fs_from_list:
print(element)

4.6.7 - Frozenset Length

The len() function returns the number of elements in a frozenset:

length = len(fs_from_list)

4.6.8 - Frozenset to Other Collections

Frozensets can be converted to other collection types like lists or sets:

# Converting to a list
fs_list = list(fs_from_list)

# Converting to a set
fs_set = set(fs_from_list)

4.6.9 - Hashability of Frozensets

Frozensets are hashable, making them suitable for use as keys in dictionaries:

fs_key_dict = {frozenset([1, 2, 3]): "value"}

4.6.10 - Summary Table of Frozenset Operations and Characteristics

Operation/CharacteristicDescriptionExample
Creating FrozensetsDefine a frozenset using frozenset().fs = frozenset([1, 2, 3])
ImmutabilityFrozensets cannot be modified after creation.
Set OperationsUnion, intersection, difference, symmetric difference.`union = a
Checking MembershipCheck if an item is in the frozenset.if 1 in fs: ...
Comparing FrozensetsCompare frozensets using relational operators.fs1 < fs2
Iterating Over FrozensetsIterate over elements in a frozenset.for x in fs: print(x)
Frozenset LengthGet the number of elements in a frozenset.len(fs)
Converting to Other CollectionsConvert frozensets to lists or sets.list(fs), set(fs)
HashabilityFrozensets can be used as keys in dictionaries.fs_dict = {fs: "value"}