Skip to content

Clarify Python 3.15 requirement for set comprehension unpacking in tutorial #162823

Not planned

Description

@newtonjose

Documentation URL

https://docs.python.org/3/tutorial/datastructures.html#sets

Documentation

Affected pages

Summary

The Sets tutorial on /3/ (currently Python 3.15) demonstrates PEP 798 iterable unpacking inside a set comprehension without noting that this syntax requires Python 3.15:

>>> fruits = [{'apple', 'avocado', 'apricot'}, {'banana', 'blueberry'}]
>>> {*fruit for fruit in fruits}

Readers on Python 3.14 following the default /3/ URL encounter:

  File "<python-input-0>", line 1
    {*fruit for fruit in fruits}
     ^^^^^^
SyntaxError: iterable unpacking cannot be used in comprehension

Additionally, the Python 3.14 version of Section 5.4 does not show a 3.14-compatible way to unpack or combine a sequence of sets.

Suggested changes

  1. main and 3.15 (Doc/tutorial/datastructures.rst):
    Add a brief .. versionadded:: 3.15 directive (or inline note) stating that * unpacking inside comprehensions was introduced in Python 3.15.

  2. 3.14 (Doc/tutorial/datastructures.rst, if within tutorial scope):
    Add a brief example after the set comprehension block showing how to combine multiple sets in 3.14:

Similarly to list comprehensions, set comprehensions are also supported::

   >>> a = {x for x in 'abracadabra' if x not in 'abc'}
   >>> a
   {'r', 'd'}

Multiple sets can also be combined using unpacking or a nested comprehension::

   >>> fruits = [{'apple', 'avocado', 'apricot'}, {'banana', 'blueberry'}]
   >>> {item for fruit in fruits for item in fruit}
   {'apple', 'avocado', 'apricot', 'banana', 'blueberry'}
   >>> set().union(*fruits)
   {'apple', 'avocado', 'apricot', 'banana', 'blueberry'}

Activity

  1. added
    docs
    on Nov 28, 2026
  2. picnixz commented on Nov 28, 2026

    @picnixz
    Member

    I don't think we actually add version-specific information in tutorials. We shouldn't assume that the user is too dumb to see that they're on the "stable version". And for 3.14, I don't think we need to make it more complex.

  3. newtonjose commented on Nov 29, 2026

    @newtonjose
    Author

    Fair enough, @picnixz. It makes sense to keep the tutorial minimal and version-agnostic.

    For the record, though, this isn't about users failing to notice the version selector. Search engines route directly to /3/ (3.15), while OS-packaged and default local Python installations routinely lag 1–2 minor versions behind. When a new user copies an example from the official tutorial and hits a SyntaxError on new PEP 798 syntax, neither the traceback nor the page signals version skew—so they burn time troubleshooting valid-looking code before realizing it's a 3.14 vs. 3.15 mismatch.

    I'm leaving that UX context here for reference. Closing this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docs

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions