What's the difference?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Jun 10 21:14:12 EDT 2010


On Thu, 10 Jun 2010 15:27:58 -0700, Martin wrote:

> On Jun 10, 11:13 pm, Anthony Papillion <papill... at gmail.com> wrote:
>> Thank you Emile and Thomas! I appreciate the help. MUCH clearer now.
> 
> Also at a guess I think perhaps you wrote the syntax slightly wrong
> (square brackets)...you might want to look up "list comprehension"


You guess wrong. Python also has generator expressions, which are written 
using round brackets instead of square.

In short, the syntax:

it = (expr for x in seq if cond)

is equivalent to:

def generator():
    for x in seq:
        if cond:
            yield expr

it = generator()


except it doesn't create the generator function first.


-- 
Steven



More information about the Python-list mailing list