yet another list comprehension question

namekuseijin namekuseijin at gmail.com
Tue May 5 23:17:28 EDT 2009


2009/5/5 Ricardo Aráoz <ricaraoz at gmail.com>:
> 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)]

Why that extra "for"?  KISS

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

"in" works perfectly well for any sequence, including strings.



More information about the Python-list mailing list