list iteration if statement

Steve Holden steve at holdenweb.com
Fri Jan 2 21:09:48 EST 2009


alex goretoy wrote:
> Hello All,
> 
> I'm doing this in my code
> 
> [[v.append(j) for j in i] for i in self.value]
> 
> if works and all, but I need to add a if statement in the mix. Can't
> seem to remember the syntax to do so and everything I've tried seems to
> fail. How do I add a check to see if j is not int("0") then append to v
> list? Thank you in advance. -A

Who, tiger. It "works" for a value of "works" that involves creating two
lists. One is the one you want, referenced by v, and the other is the
value if the list comprehension, which will be a list full of lists of
all the None values returned by those append() calls. But I presume you
are throwing that second list away ...

See, a list comprehension is intended to create a list. So what you
should have used (assuming v was the empty list before you started) was

v = [[j for j in i] for i in self.value]

Further, when you say 'j is not int("0")', do you actually mean that a
is not in integer with the value 0? Assuming you do then what you need is

v = [[j for j in i if not j] for i in self.value]

or, more pedantically

v = [[j for j in i if j==0] for i in self.value]

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list