yet another list comprehension question

Ricardo Aráoz ricaraoz at gmail.com
Tue May 5 11:12:23 EDT 2009


namekuseijin wrote:
> On May 4, 9:15 am, David Robinow <drobi... at gmail.com> wrote:
>   
>> On Mon, May 4, 2009 at 2:33 AM, namekuseijin
>>
>> <namekuseijin.nos... at gmail.com> wrote:
>>     
>>>>>> ls = [(1,2), (3,4), (5, None), (6,7), (8, None)]
>>>>>> [(x,y) for (x,y) in ls if y]
>>>>>>             
>>> [(1, 2), (3, 4), (6, 7)]
>>>       
>> Nope. That filters out 0 as well as None. Not what the OP asked for.
>>     
>
> True.  I'm still a C programmer at heart I guess.  ah, the flexibility
> of 0... ;)
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>   
This seems to work for any length tuples :

>>> a = [(1,2), (3,4, 'goes'), (5,None), (6,7, 8, 'as', None), (8,
None), (9, 0)]
>>> [tup for tup in a if not [e for e in tup if e == None]]
[(1, 2), (3, 4, 'goes'), (9, 0)]


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090505/fab16dc4/attachment.html>


More information about the Python-list mailing list