Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A frozenset is a built-in Python data type representing an immutable, unordered collection of unique, hashable elements. It serves as the immutable counterpart to Python’s standard set type, meaning its internal state and element composition cannot be modified after instantiation. Because a frozenset is immutable and Python strictly requires all of its elements to be hashable, the frozenset itself is hashable. This characteristic allows a frozenset to be used as a dictionary key or as an element within another set—operations that are impossible with a standard mutable set.

Instantiation

Because it is immutable, a frozenset must be populated at the time of creation using the frozenset() constructor. The constructor accepts a single optional iterable argument. Attempting to instantiate a frozenset with unhashable elements (such as lists or dictionaries) immediately raises a TypeError.

# Empty frozenset
empty_fs = frozenset()


# Instantiation from an iterable (list)

# Duplicates are automatically discarded
fs_from_list = frozenset([1, 2, 3, 2, 1])  

# Result: frozenset({1, 2, 3})


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

# Result: frozenset({'h', 'e', 'l', 'o'})


# Utilizing hashability: frozenset as a dict key and set element
fs_key = frozenset(['a', 'b'])
my_dict = {fs_key: "value"}
my_set = {fs_key, frozenset([1, 2])}

Technical Characteristics

  • Immutability: Once created, elements cannot be added, removed, or altered.
  • Hashability: Unlike a standard set, a frozenset implements a __hash__() method. Because Python enforces that all elements within any set type must be hashable, a frozenset is guaranteed to evaluate to a constant hash value.
  • Unordered: Elements do not maintain insertion order. Consequently, a frozenset does not support indexing, slicing, or other sequence-like behaviors.
  • Uniqueness: It strictly enforces element uniqueness. Duplicate values passed during instantiation are ignored.

Supported Operations

A frozenset supports all non-mutating mathematical set operations. These operations evaluate the elements and return a new frozenset or a boolean value, leaving the original objects unchanged.
fs_a = frozenset([1, 2, 3])
fs_b = frozenset([3, 4, 5])


# Union (Returns elements in either set)
fs_a | fs_b                  # frozenset({1, 2, 3, 4, 5})
fs_a.union(fs_b)


# Intersection (Returns elements common to both sets)
fs_a & fs_b                  # frozenset({3})
fs_a.intersection(fs_b)


# Difference (Returns elements in fs_a not in fs_b)
fs_a - fs_b                  # frozenset({1, 2})
fs_a.difference(fs_b)


# Symmetric Difference (Returns elements in either set, but not both)
fs_a ^ fs_b                  # frozenset({1, 2, 4, 5})
fs_a.symmetric_difference(fs_b)


# Relational Checks (Returns boolean)
fs_a <= fs_b                 # False (issubset)
fs_a >= frozenset([1, 2])    # True  (issuperset)
fs_a.isdisjoint(fs_b)        # False (True if no common elements)

Unsupported Operations

Because a frozenset is immutable, it lacks the in-place mutating methods found on a standard set. Attempting to invoke any of the following methods will raise an AttributeError:
fs = frozenset([1, 2, 3])


# The following methods are undefined for frozenset:
fs.add(4)
fs.remove(2)
fs.discard(1)
fs.pop()
fs.clear()
fs.update([4, 5])
Master Python with Deep Grasping Methodology!Learn More