In Python 3.15 and newer, frozendict() creates an immutable mapping: it stores key/value pairs like a dict, but nothing can be added, changed, or removed after it is created. It was added by PEP 814.
Because it cannot change, a frozendict whose values are all hashable can itself be hashed, so it can be a dict key or a set item, much like frozenset is to set. On older versions the name does not exist; the usual read-only substitute is types.MappingProxyType, which blocks changes but is not hashable.
frozendict is a built-in type (a class) that you call like a function. On Python 3.15+ it is always available without an import, like every other built-in. New in Python 3.15. On Python 3.14 and older, using it raises NameError: name 'frozendict' is not defined, so the examples below include a fallback. This page sticks to the basic behaviour; check the official Python 3.15 documentation for full details such as the exact method list.
# On Python 3.15+:
frozendict
1 2
{'lang': 'python', 'level': 2}
# On Python 3.14 and older:
frozendict not available; using dict copies
1 2
{'lang': 'python', 'level': 2}
Lines starting with # only label which Python version prints the lines below them; the program does not print them.
How It Works
A bare name as a probe. The statement frozendict does nothing except raise NameError when the type is missing.
New object per change.frozendict(frozen, level=2) copies the pairs and overrides one key, like dict(d, key=value).
Original untouched.frozen["level"] is still 1 while updated["level"] is 2.
Back to a dict.dict(updated) gives a normal, mutable dict when you need one.
Frequently Asked Questions
frozendict is new in Python 3.15 (PEP 814). On 3.14 and older, the name is not defined and you get NameError.
For mappings that must not change, such as configuration or constants, and for mappings that need to be hashable, for example as dict keys or cache keys.
types.MappingProxyType(dict(data)) gives a read-only view. For a hashable key, use frozenset(data.items()) or a tuple of sorted items.
Only if all of its values are hashable. A frozendict that contains a list value cannot be hashed.
You cannot change it in place. Build a new one with the updated key, or convert it to a dict, change that, and freeze it again.