frozenset()

frozenset([iterable])
Returns: frozenset · Updated March 13, 2026 · Built-in Functions
built-in set immutable hashable

The frozenset() function creates an immutable set object from an iterable. Unlike regular sets, frozensets cannot be modified after creation — you cannot add or remove elements. This immutability makes frozensets hashable, meaning they can be used as dictionary keys or stored in other sets.

Syntax

frozenset([iterable])

Parameters

ParameterTypeDefaultDescription
iterableiterableAny iterable (list, tuple, string, dict, set, etc.). If omitted, creates an empty frozenset.

Examples

Basic usage

# Create a frozenset from a list
fruits = frozenset(["apple", "banana", "cherry"])
print(fruits)
# frozenset({'apple', 'banana', 'cherry'})

# Create an empty frozenset
empty = frozenset()
print(empty)
# frozenset()

From a string

# Each character becomes an element (no duplicates)
letters = frozenset("hello")
print(letters)
# frozenset({'h', 'e', 'l', 'o'})

From a dictionary

# Only the keys are used
data = frozenset({"a": 1, "b": 2, "c": 3})
print(data)
# frozenset({'a', 'b', 'c'})

Why Use Frozensets?

Hashability

Regular sets are mutable and therefore not hashable:

# This works
regular_set = {1, 2, 3}
my_dict = {regular_set: "value"}  # TypeError: unhashable type 'set'

# Frozensets are hashable
frozen = frozenset([1, 2, 3])
my_dict = {frozen: "value"}
print(my_dict)
# {frozenset({1, 2, 3}): 'value'}

As dictionary keys

# Use frozensets as keys for complex lookups
lookup = {
    frozenset(["red", "blue"]): "purple",
    frozenset(["red", "yellow"]): "orange",
}

print(lookup[frozenset(["red", "blue"])])
# purple

Nested sets

# You cannot have a set of sets, but you can have a set of frozensets
set_of_sets = {frozenset([1, 2]), frozenset([3, 4])}
print(set_of_sets)
# {frozenset({1, 2}), frozenset({3, 4})}

Frozenset Methods

Frozensets support most set methods except those that modify the set:

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

# These work (read-only operations)
print(len(f))           # 5
print(2 in f)           # True
print(f.copy())        # frozenset({1, 2, 3, 4, 5})
print(f.isdisjoint(frozenset([6, 7])))  # True
print(f.issubset(frozenset([1, 2, 3, 4, 5, 6])))  # True

# These would fail (modification operations)
# f.add(6)        # AttributeError
# f.remove(1)     # AttributeError
# f.clear()      # AttributeError

Common Patterns

Using frozensets as set elements

# Track which users have which permissions
user_permissions = {
    "alice": frozenset(["read", "write"]),
    "bob": frozenset(["read"]),
}

# Check if a user has a specific permission
print("write" in user_permissions["alice"])
# True

Grouping with frozenset keys

from collections import defaultdict

# Group items by their characteristics
items = [
    {"name": "apple", "color": "red", "size": "small"},
    {"name": "banana", "color": "yellow", "size": "large"},
    {"name": "cherry", "color": "red", "size": "small"},
]

groups = defaultdict(list)
for item in items:
    key = frozenset([item["color"], item["size"]])
    groups[key].append(item["name"])

print(groups)
# defaultdict(<class 'list'>, 
#   {frozenset({'red', 'small'}): ['apple', 'cherry'], 
#    frozenset({'yellow', 'large'}): ['banana']})

See Also