frozenset() is a built-in Python function that creates an **immutable** set — a hashable, unchangeable collection of unique elements that can be used as dictionary keys, set elements, or in other hash-based structures where mutability would break things. Unlike regular set, frozensets cannot be modified after creation (no add/remove/update), making them safe for use in sets, dict keys, caching, and parallel processing. In 2026, frozenset remains essential in data science (unique key hashing in pandas/Polars grouping), software engineering (immutable configs, frozen state), and functional programming — especially when combined with Dask Bags, Polars structs, or caching mechanisms where mutability could cause race conditions or hash inconsistencies.
Here’s a complete, practical guide to using frozenset() in Python: creation & basic operations, hashability & immutability, real-world patterns (earthquake event deduplication, grouping keys, caching), and modern best practices with type hints, performance, comparison to set/tuple, and integration with Dask/Polars/pandas/xarray.
Creating frozensets — from iterables, sets, or empty.
# From list/tuple (removes duplicates)
fs1 = frozenset([1, 2, 2, 3, 4])
print(fs1) # frozenset({1, 2, 3, 4})
# From set (copy as immutable)
s = {1, 2, 3}
fs2 = frozenset(s)
print(fs2) # frozenset({1, 2, 3})
# Empty frozenset
fs_empty = frozenset()
print(fs_empty) # frozenset()
# From string (unique characters)
fs_chars = frozenset("hello")
print(fs_chars) # frozenset({'h', 'e', 'l', 'o'})
Hashability & immutability — key advantages over regular set.
# Can be used as dict key
d = {frozenset([1, 2, 3]): "group A"}
print(d[frozenset([1, 2, 3])]) # group A
# Can be added to sets
s = {frozenset([1, 2]), frozenset([3, 4])}
print(frozenset([1, 2]) in s) # True
# Immutable: cannot modify
# fs1.add(5) # AttributeError: 'frozenset' object has no attribute 'add'
Real-world pattern: earthquake event deduplication & grouping — use frozenset for composite keys.
import pandas as pd
# Sample earthquake data with possible duplicates
df = pd.DataFrame({
'event_id': [1, 2, 1, 3],
'mag': [7.1, 6.8, 7.1, 6.5],
'place': ['Japan', 'Chile', 'Japan', 'Alaska'],
'time': ['2025-01-01', '2025-01-02', '2025-01-01', '2025-01-03']
})
# Create composite key as frozenset (id + time + place)
df['unique_key'] = df.apply(
lambda row: frozenset([row['event_id'], row['time'], row['place']]), axis=1
)
# Deduplicate using frozenset key
deduped = df.drop_duplicates(subset='unique_key')
print(f"Original: {len(df)}, Deduped: {len(deduped)}")
# Group by frozenset of countries (example: multi-country events)
df['countries'] = df['place'].str.split(',').apply(frozenset)
grouped = df.groupby('countries')['mag'].mean()
print(grouped)
Best practices for frozenset() in Python & data workflows. Use frozenset for hashable sets — when you need sets as dict keys or set elements. Modern tip: use Polars pl.col('list_col').list.unique() — for unique lists; Dask for distributed grouping. Prefer frozenset over tuple of sorted elements — for unordered uniqueness. Use frozenset for composite keys — when order doesn’t matter. Add type hints — def group_key(ids: list[int]) -> frozenset[int]. Use frozenset in caching — @cache def func(fs: frozenset): .... Use frozenset for membership testing — if item in fs (O(1)). Avoid large frozensets — hashing cost grows with size. Use frozenset.union() — for set operations. Use frozenset.intersection() — for common elements. Use frozenset.difference() — for set difference. Use frozenset.symmetric_difference() — for XOR. Use frozenset.isdisjoint() — fast check. Use frozenset.issubset()/issuperset() — subset/superset tests. Use frozenset.copy() — shallow copy. Use frozenset() == set() — equality check (order-independent).
frozenset() creates immutable, hashable sets — perfect for dictionary keys, set elements, caching, and deduplication where mutability would break things. In 2026, use for composite keys, unique grouping, and integrate with pandas/Polars/Dask for scalable data operations. Master frozenset, and you’ll handle uniqueness and hashing cleanly and safely in any Python project.
Next time you need an immutable set — use frozenset(). It’s Python’s cleanest way to say: “Give me a set I can hash and share — without ever changing it.”