[Tutor] List comprehension possible with condition statements?

Alan Gauld alan.gauld at btinternet.com
Thu Mar 4 02:19:13 CET 2010


"Steven D'Aprano" <steve at pearwood.info> wrote

> List comps can include *any* comparison:
> 
> [x+1 for x in data if (3*x+2)**2 > 100*x or x < -5]

Sure, but the wording suggested (maybe wrongly) that the OP 
was a real beginner and so the concept of an expression 
was likely to be foreign. Sticking with equalty or inequality 
seemed likely to be the most familiar test to him/her.

Trying to explain the differnce between is and == seemed 
like it would be a diversion from the primary objective, 
namely how to filter a list comp.

However I guess this discussion has covered that topic too
now, so once again we have managed to kill several 
sparrows with one round of buckshot... :-)


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list