content_image/699378.jpg
## 6.2.4 Displays for lists, sets and dictionaries 

For constructing a list, a set or a dictionary Python provides special syntax called “displays”, each of them in two flavors: 

• either the container contents are listed explicitly, or 

• they are computed via a set of looping and filtering instructions, called a comprehension . 

Common syntax elements for comprehensions are: 

<img src='content_image/699378.jpg'>

The comprehension consists of a single expression followed by at least one$_{for}$ clause and zero or more$_{for}$ or$_{if}$ clauses. In this case, the elements of the new container are those that would be produced by considering each of the for or if clauses a block, nesting from left to right, and evaluating the expression to produce an element each time the innermost block is reached. 

However, aside from the iterable expression in the leftmost$_{for}$ clause, the comprehension is executed in a separate implicitly nested scope. This ensures that names assigned to in the target list don’t “leak” into the enclosing scope. 

The iterable expression in the leftmost$_{for}$ clause is evaluated directly in the enclosing scope and then passed as an argument to the implicitly nested scope. Subsequent$_{for}$ clauses and any filter condition in the leftmost$_{for}$ clause cannot be evaluated in the enclosing scope as they may depend on the values obtained from the leftmost iterable. For example:$_{[x*y for x in range(10) for y in range(x, x+10)]}$ . 

To ensure the comprehension always results in a container of the appropriate type,$_{yield}$ and$_{yield from}$ex- pressions are prohibited in the implicitly nested scope. 

Since Python 3.6, in an$_{async def}$function, an$_{async for}$clause may be used to iterate over a asynchronous iterator . A comprehension in an$_{async def}$function may consist of either a$_{for}$ or$_{async for}$clause following the leading expression, may contain additional$_{for}$ or$_{async for}$clauses, and may also use$_{await}$expressions. If a comprehension contains either$_{async for}$clauses or$_{await}$expressions it is called an asynchronous compre- hension . An asynchronous comprehension may suspend the execution of the coroutine function in which it appears. See also PEP 530 . 

New in version 3.6: Asynchronous comprehensions were introduced. 

Changed in version 3.8:$_{yield}$ and$_{yield from}$prohibited in the implicitly nested scope. 

## 6.2.5 List displays 

A list display is a possibly empty series of expressions enclosed in square brackets: 

list_display ::= "[" [ starred_list | comprehension ] "]" 

A list display yields a new list object, the contents being specified by either a list of expressions or a comprehension. When a comma-separated list of expressions is supplied, its elements are evaluated from left to right and placed into the list object in that order. When a comprehension is supplied, the list is constructed from the elements resulting from the comprehension. 

