list iteration if statement

Steve Holden steve at holdenweb.com
Fri Jan 2 23:04:27 EST 2009


alex goretoy wrote:
> 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]
> 
No, it wasn't. You should *not* be modifying v in the list comprehension!

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

If the value is stored as a string them you should be testing for "0",
not 0.

> 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], [None]]
> 
If you just want to append all those lists to v then you should just say

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

You are getting the None values because the append method returns None.

regards
 Steve

> You guys rock, thanks for helping me learn python.
> 
> On Sat, Jan 3, 2009 at 2:09 AM, Steve Holden <steve at holdenweb.com
> <mailto:steve at holdenweb.com>> 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              http://www.holdenweb.com/
> 
>     --
>     http://mail.python.org/mailman/listinfo/python-list
> 
> 
> 
> 
> -- 
> А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я
> а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я
> 
> 
> ------------------------------------------------------------------------
> 
> --
> http://mail.python.org/mailman/listinfo/python-list


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