[Tutor] Ingenious script (IMO)

Kent Johnson kent37 at tds.net
Mon Aug 6 23:47:10 CEST 2007


Ken Oliver wrote:
> I seem to stumble often with things like
> 
> nz = [ (k,v) for k,v in lst if v!=0]
> 
> I have not been able to wrap my brain around the parentheses. I see
> it
as reasonable to have the () around the k,v in the tuple in the first
instance, but I feel like they should also be around k,v in the second
instance (the for clause).

You can write it as [ (k,v) for (k,v) in lst if v!=0] if you prefer.

> Now can you do it all in one statement? Hehe. Just teasing.

I hope it's obvious how to do that...

Kent


More information about the Tutor mailing list