list iteration if statement

alex goretoy aleksandr.goretoy at gmail.com
Fri Jan 2 21:43:46 EST 2009


rather, how do I suppress the output of the list with all None in it?

>>> l=[['3'], ['0', '4'], ['0', '1'], ['0']]
>>> v=[]
>>> [[v.append(j)for j in i if j != "0"] for i in l]
[[None], [None], [None], []]
>>> v
['39', '32', '1']
>>>

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



-- 
А-Б-В-Г-Д-Е-Ё-Ж-З-И-Й-К-Л-М-Н-О-П-Р-С-Т-У-Ф-Х-Ц-Ч-Ш-Щ-Ъ-Ы-Ь-Э-Ю-Я
а-б-в-г-д-е-ё-ж-з-и-й-к-л-м-н-о-п-р-с-т-у-ф-х-ц-ч-ш-щ-ъ-ы-ь-э-ю-я
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090103/265d8717/attachment.html>


More information about the Python-list mailing list