[Tutor] Better structure?

Alan Gauld alan.gauld at freenet.co.uk
Fri Feb 4 10:39:40 CET 2005


> Although, (and this will be rough) a list comprehension would be
> probably do the same thing
> j=[1, 2,3,4, 5,6,7,8]
> 
> q = [if not item % 2 for item in j]

I think you mean:

q = [ item for item in j if item % 2]

item % 2 will return zero = false on even numbers, so 
your test is true for odd numbers. No need for 'not'.

Alan G.
 



More information about the Tutor mailing list