@@ -69,6 +69,8 @@ Summary -- Release highlights
6969 profiling tools <whatsnew315-profiling-package>`
7070* :pep: `799 `: :ref: `Tachyon: High frequency statistical sampling profiler
7171 profiling tools <whatsnew315-sampling-profiler>`
72+ * :pep: `798 `: :ref: `Unpacking in Comprehensions
73+ <whatsnew315-unpacking-in-comprehensions>`
7274* :pep: `686 `: :ref: `Python now uses UTF-8 as the default encoding
7375 <whatsnew315-utf8-default>`
7476* :pep: `782 `: :ref: `A new PyBytesWriter C API to create a Python bytes object
@@ -187,6 +189,45 @@ available output formats, profiling modes, and configuration options.
187189
188190(Contributed by Pablo Galindo and László Kiss Kollár in :gh: `135953 ` and :gh: `138122 `.)
189191
192+ .. _whatsnew315-unpacking-in-comprehensions :
193+
194+ :pep: `798 `: Unpacking in Comprehensions
195+ ---------------------------------------
196+
197+ List, set, and dictionary comprehensions, as well as generator expressions, now
198+ support unpacking with ``* `` and ``** ``. This extends the unpacking syntax
199+ from :pep: `448 ` to comprehensions, providing a new syntax for combining an
200+ arbitrary number of iterables or dictionaries into a single flat structure.
201+ This new syntax is a direct alternative to nested comprehensions,
202+ :func: `itertools.chain `, and :meth: `itertools.chain.from_iterable `. For
203+ example::
204+
205+ >>> lists = [[1, 2], [3, 4], [5]]
206+ >>> [*L for L in lists] # equivalent to [x for L in lists for x in L]
207+ [1, 2, 3, 4, 5]
208+
209+ >>> sets = [{1, 2}, {2, 3}, {3, 4}]
210+ >>> {*s for s in sets} # equivalent to {x for s in sets for x in s}
211+ {1, 2, 3, 4}
212+
213+ >>> dicts = [{'a': 1}, {'b': 2}, {'a': 3}]
214+ >>> {**d for d in dicts} # equivalent to {k: v for d in dicts for k,v in d.items()}
215+ {'a': 3, 'b': 2}
216+
217+ Generator expressions can similarly use unpacking to yield values from multiple
218+ iterables::
219+
220+ >>> gen = (*L for L in lists) # equivalent to (x for L in lists for x in L)
221+ >>> list(gen)
222+ [1, 2, 3, 4, 5]
223+
224+ This change also extends to asynchronous generator expressions, such that, for
225+ example, ``(*a async for a in agen()) `` is equivalent to ``(x async for a in
226+ agen() for x in a) ``.
227+
228+ .. seealso :: :pep:`798` for further details.
229+
230+ (Contributed by Adam Hartz in :gh: `143055 `.)
190231
191232.. _whatsnew315-improved-error-messages :
192233
0 commit comments