List displays

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

Syntax:

[]

[ expression [ , expression]… [,] ]

[ expression list [ for target list in expression list ]… [ if expression ]… ]

(the outermost brackets are part of the syntax)

A list display yields a new list object. Its contents are specified by providing either a list of expressions or a list 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 list comprehension is supplied, it 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 list 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 a list element each time the innermost block is reached.

Note: In the current versions, a list comprehension leaks the control variables of each for it contains into the containing scope. However, this behavior is deprecated, and will change in a future release.

 

Comment:

The last syntax line is incorrect. The first item must be a for, but the rest can be either for or if. E.g. [a for b in c if d in e for f in g] is syntactically correct (if not very meaningful, of course).

Posted by infogami

A Django site. rendered by a django application. hosted by webfaction.