We're back after a server migration that caused effbot.org to fall over a bit harder than expected. Expect some glitches.

generator-expressions

A generator expression is a compact generator notation in parentheses, similar to list comprehensions.

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

A generator expression yields a new generator object. It consists of a single expression followed by at least one for clause and zero or more for or if clauses. The iterating values of the new generator 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 yield a value that is reached the innermost block for each iteration.

Variables used in the generator expression are evaluated lazily when the next method is called for generator object (in the same fashion as normal generators). However, the leftmost for clause is immediately evaluated so that error produced by it can be seen before any other possible error in the code that handles the generator expression. Subsequent for clauses cannot be evaluated immediately since they may depend on the previous for loop. For example: “(x*y for x in range(10) for y in bar(x))”.

The parentheses can be omitted when a generator expression is used as the only argument to a call. See calls for details.