# \`frozenset\` and \`frozendict\` comprehensions

**URL:** <https://discuss.python.org/t/frozenset-and-frozendict-comprehensions/101584>\
**Category:** Ideas\
**Created:** [August 7, 2025, 6:32pm UTC](https://discuss.python.org/t/frozenset-and-frozendict-comprehensions/101584 "2025-08-07T18:32:59Z")\
**Posts on this page:** 1\
**Showing post:** 63

<div class="post-metadata">

**Author:** ![vstinner](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vstinner/32/15130_2.png) [@vstinner](https://discuss.python.org/u/vstinner)\
**Post date:** [July 10, 2026, 6:36pm UTC](https://discuss.python.org/t/frozenset-and-frozendict-comprehensions/101584/63 "2026-07-10T18:36:34Z")

</div>

An alternative to changing the Python grammar for a new syntax is to add new efficient `dict` and `set` methods to convert to `frozendict` and `frozenset`. See [Add a dict method to convert to a frozendict?](https://discuss.python.org/t/add-a-dict-method-to-convert-to-a-frozendict/108102) discussion.

Regular `dict` and `set` are used to build a collection, and then `.take_frozendict()` or `.take_frozenset()` freezes the object (convert to `frozendict`/`frozenset`). These functions clear the temporary `dict`/`set`.

For example, `frozendict({'x': 1, 'y': 2})` can be written `{'x': 1, 'y': 2}.take_frozendict()`. So instead of adding a prefix (multiple prefixes have been proposed so far!), it does add a… suffix 🙂

Some examples.

Frozendict literal, when `frozendict(key=value, ...)` syntax cannot be used:

```diff
-_capability_names = frozendict({
+_capability_names = {
     _curses.KEY_A1: 'ka1',
     _curses.KEY_A3: 'ka3',
     _curses.KEY_B2: 'kb2',
     ...
- })
+ }.take_frozendict()

```

Frozenset literal:

```diff
- _ID_KEYWORDS = frozenset({"True", "False", "None"})
+ _ID_KEYWORDS = {"True", "False", "None"}.take_frozenset()

```

As a bonus, it can be more in more cases, like convert a list-comprehension to a tuple:

```diff
- return tuple([_asdict_inner(v, dict_factory) for v in obj])
+ return [_asdict_inner(v, dict_factory) for v in obj].take_tuple()

```

Or convert a set-comprehension to a frozenset:

```diff
-_ATTRIB_DENY_LIST = frozenset({
+_ATTRIB_DENY_LIST = {
     name.removeprefix("assert_")
     for name in dir(NonCallableMock)
     if name.startswith("assert_")
-})
+}.take_frozenset()

```

See my [pull request](https://github.com/vstinner/cpython/pull/16) for more examples. It also adds `list.take_tuple()` method.

---

_[View the full topic](https://discuss.python.org/t/frozenset-and-frozendict-comprehensions/101584)._
