nested list comprehension and if clauses

Alex Martelli aleax at
Thu Jun 28 08:09:49 CEST 2007

Paul Rubin <> wrote:

> aleax at (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:-).


More information about the Python-list mailing list