@@ -266,17 +266,19 @@ called "displays", each of them in two flavors:
266266Common syntax elements for comprehensions are:
267267
268268.. productionlist :: python-grammar
269- comprehension: `assignment_expression ` `comp_for `
269+ comprehension: `flexible_expression ` `comp_for `
270270 comp_for: ["async"] "for" `target_list ` "in" `or_test ` [`comp_iter `]
271271 comp_iter: `comp_for ` | `comp_if `
272272 comp_if: "if" `or_test ` [`comp_iter `]
273273
274274The comprehension consists of a single expression followed by at least one
275- :keyword: `!for ` clause and zero or more :keyword: `!for ` or :keyword: `!if ` clauses.
276- In this case, the elements of the new container are those that would be produced
277- by considering each of the :keyword: `!for ` or :keyword: `!if ` clauses a block,
278- nesting from left to right, and evaluating the expression to produce an element
279- each time the innermost block is reached.
275+ :keyword: `!for ` clause and zero or more :keyword: `!for ` or :keyword: `!if `
276+ clauses. In this case, the elements of the new container are those that would
277+ be produced by considering each of the :keyword: `!for ` or :keyword: `!if `
278+ clauses a block, nesting from left to right, and evaluating the expression to
279+ produce an element each time the innermost block is reached. If the expression
280+ is starred, the result will instead be unpacked to produce zero or more
281+ elements.
280282
281283However, aside from the iterable expression in the leftmost :keyword: `!for ` clause,
282284the comprehension is executed in a separate implicitly nested scope. This ensures
@@ -321,6 +323,9 @@ See also :pep:`530`.
321323 asynchronous functions. Outer comprehensions implicitly become
322324 asynchronous.
323325
326+ .. versionchanged :: next
327+ Unpacking with the ``* `` operator is now allowed in the expression.
328+
324329
325330.. _lists :
326331
@@ -396,8 +401,8 @@ enclosed in curly braces:
396401.. productionlist :: python-grammar
397402 dict_display: "{" [`dict_item_list ` | `dict_comprehension `] "}"
398403 dict_item_list: `dict_item ` ("," `dict_item `)* [","]
404+ dict_comprehension: `dict_item ` `comp_for `
399405 dict_item: `expression ` ":" `expression ` | "**" `or_expr `
400- dict_comprehension: `expression ` ":" `expression ` `comp_for `
401406
402407A dictionary display yields a new dictionary object.
403408
@@ -419,10 +424,21 @@ earlier dict items and earlier dictionary unpackings.
419424.. versionadded :: 3.5
420425 Unpacking into dictionary displays, originally proposed by :pep: `448 `.
421426
422- A dict comprehension, in contrast to list and set comprehensions, needs two
423- expressions separated with a colon followed by the usual "for" and "if" clauses.
424- When the comprehension is run, the resulting key and value elements are inserted
425- in the new dictionary in the order they are produced.
427+ A dict comprehension may take one of two forms:
428+
429+ - The first form uses two expressions separated with a colon followed by the
430+ usual "for" and "if" clauses. When the comprehension is run, the resulting
431+ key and value elements are inserted in the new dictionary in the order they
432+ are produced.
433+
434+ - The second form uses a single expression prefixed by the ``** `` dictionary
435+ unpacking operator followed by the usual "for" and "if" clauses. When the
436+ comprehension is evaluated, the expression is evaluated and then unpacked,
437+ inserting zero or more key/value pairs into the new dictionary.
438+
439+ Both forms of dictionary comprehension retain the property that if the same key
440+ is specified multiple times, the associated value in the resulting dictionary
441+ will be the last one specified.
426442
427443.. index :: pair: immutable; object
428444 hashable
@@ -439,6 +455,8 @@ prevails.
439455 the key. Starting with 3.8, the key is evaluated before the value, as
440456 proposed by :pep: `572 `.
441457
458+ .. versionchanged :: next
459+ Unpacking with the ``** `` operator is now allowed in dictionary comprehensions.
442460
443461.. _genexpr :
444462
@@ -453,7 +471,7 @@ Generator expressions
453471A generator expression is a compact generator notation in parentheses:
454472
455473.. productionlist :: python-grammar
456- generator_expression: "(" `expression ` `comp_for ` ")"
474+ generator_expression: "(" `flexible_expression ` `comp_for ` ")"
457475
458476A generator expression yields a new generator object. Its syntax is the same as
459477for comprehensions, except that it is enclosed in parentheses instead of
0 commit comments