Python frozensets are immutable versions of sets. Like sets, frozensets are collections of unique elements, but unlike sets, frozensets cannot be modified after they are created. Frozensets are hashable, meaning they can be used as keys in dictionaries or as elements of other sets.
A frozenset in Python is created using the frozenset()
function, which can take an iterable as an argument.
my_frozenset = frozenset([1, 2, 3])
empty_frozenset = frozenset() # Create an empty frozenset
Frozensets support all the standard set operations, but since they are immutable, operations that would modify a set will return a new frozenset instead.
Union (|
): Combine two frozensets to include all elements from both.
fs1 = frozenset([1, 2, 3])
fs2 = frozenset([3, 4, 5])
union_fs = fs1 | fs2 # frozenset({1, 2, 3, 4, 5})
Intersection (&
): Get elements that are common to both frozensets.
intersection_fs = fs1 & fs2 # frozenset({3})
Difference (-
): Get elements that are in the first frozenset but not in the second.
difference_fs = fs1 - fs2 # frozenset({1, 2})
Symmetric Difference (^
): Get elements that are in either of the frozensets but not in both.
sym_diff_fs = fs1 ^ fs2 # frozenset({1, 2, 4, 5})
exists = 2 in fs1 # True
Frozensets share most of their methods with sets, but because they are immutable, they do not support methods that would modify the set in place.
copy()
: Return a shallow copy of the frozenset.
copy_fs = fs1.copy()
union(*others)
: Return a new frozenset with elements from the frozenset and all others.
union_fs = fs1.union(fs2, frozenset([6, 7])) # frozenset({1, 2, 3, 4, 5, 6, 7})
intersection(*others)
: Return a new frozenset with elements common to the frozenset and all others.
intersection_fs = fs1.intersection(fs2, frozenset([3, 4])) # frozenset({3})
difference(*others)
: Return a new frozenset with elements in the frozenset that are not in the others.
difference_fs = fs1.difference(fs2) # frozenset({1, 2})
symmetric_difference(other)
: Return a new frozenset with elements in either the frozenset or the other, but not both.
sym_diff_fs = fs1.symmetric_difference(fs2) # frozenset({1, 2, 4, 5})
issubset(other)
: Check if the frozenset is a subset of another.
is_subset = fs1.issubset(fs2) # False
issuperset(other)
: Check if the frozenset is a superset of another.
is_superset = fs1.issuperset(frozenset([1, 2])) # True
isdisjoint(other)
: Check if two frozensets have no elements in common.
is_disjoint = fs1.isdisjoint(frozenset([4, 5])) # True
Immutability Confusion: While frozensets themselves are immutable, they can contain mutable elements (e.g., lists) if those elements are hashable. However, if the mutable elements are modified, it can lead to unexpected behavior or errors.
mutable_fs = frozenset([[1, 2], 3]) # Raises TypeError because lists are not hashable
Set Methods Not Available: Methods that modify a set in place (e.g., add()
, remove()
, pop()
, clear()
) are not available for frozensets due to their immutability.
This comprehensive guide should help you understand Python frozensets, their features, and best practices for using them effectively in your programs.