[Tutor] build list of non-empty variables

Alan Gauld alan.gauld at btinternet.com
Thu Jul 10 02:17:23 CEST 2008


"Tim Golden" <mail at timgolden.me.uk> wrote 

>> In fact I guess you could say that the new definition of a list
>> comprehension is
>> 
>> [ generator expression]
> 
> Well, not if sure if you meant that literally

No I meant in syntactic terms.
We usually define an LC as

[ expr for vars in sequence if expr ]

or somesuch imprecise gobbledy gook ;-).

Now we can define the generator expr (syntax) as

        expr for vars in sequence if expr 

and the LC as

        [ gen expr ]

> <code>
> squares = (x * x for x in range (10))
> l = [squares]

But doesn't that generate a tuple (because of the parens)?
And if you remove the parens you cant assign to the 
variable so you have to [put it in the list literally which becomes

l = [x * x for x in range (10)]

Which is an LC...

Alan G.



More information about the Tutor mailing list