[Tutor] List comprehension possible with condition statements?

Steven D'Aprano steve at pearwood.info
Wed Mar 3 23:22:14 CET 2010


On Thu, 4 Mar 2010 05:18:40 am Alan Gauld wrote:
> "Steven D'Aprano" <steve at pearwood.info> wrote
>
> > Comparisons with None almost always should be one of:
> >
> > item is None
> > item is not None
>
> Yes, but the reason I changed it (I originally had "is not") is that
> != is a more general test for illustrating the use of 'if' within a
> LC which seemed to be the real issue within the question.

List comps can include *any* comparison:

[x+1 for x in data if (3*x+2)**2 > 100*x or x < -5]





-- 
Steven D'Aprano


More information about the Tutor mailing list