list iteration if statement

alex goretoy aleksandr.goretoy at
Sat Jan 3 03:38:54 CET 2009

Thank you Steve and MRAB,

This is what I was looking for:

 [[v.append(j) for j in i if j != 0] for i in self.value]

the value is actually stored as a string so I would need to check if it is
"0". I do have one more question about list comprehension though. After
doing this I get an unwanted list of None how do I make it disappear or not
return this list. Would I have to be modifying it in place for this to
happen? Wouldn't modifying it in place potentially overwrite some other
values? I just don't want it to return [[None], [None, None], [None, None],

You guys rock, thanks for helping me learn python.

On Sat, Jan 3, 2009 at 2:09 AM, Steve Holden <steve at> wrote:

> 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    
> --

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list