nested list comprehension and if clauses

Alex Martelli aleax at mac.com
Thu Jun 28 02:09:49 EDT 2007


Paul Rubin <http://phr.cx@NOSPAM.invalid> wrote:

> aleax at mac.com (Alex Martelli) writes:
> > > print [(m,n) for m in range(5) for n in multab(m) if m>2]
> > Sure, just place the if clause where it needs to apply (between the two
> > for clauses) [apart from the fact that this example is best expressed by
> > using range(3,5), as somebody already said;-)].
> 
> You mean
> 
>   print [(m,n) for m in range(5) if m > 2 for n in multab(m)]
> 
> Heh.  I didn't realize you could do that.  Thanks.

You're welcome (though a range(3,5) would still be better here:-).


Alex



More information about the Python-list mailing list