first non-null element in a list, otherwise None

wheres pythonmonks wherespythonmonks at gmail.com
Thu Sep 2 10:11:32 EDT 2010


Peter wrote:

>> But this can be expensive memory wise.  Is there a way to concatenate
>> generator expressions?
>
> itertools.chain()
>

Aha!

import itertools
>>> x = itertools.chain( (x for x in [None,None] if x is not None), [ None ] ).next()
>>> print x
None
>>> x = itertools.chain( (x for x in [None,7] if x is not None), [ None ] ).next()
>>> print x
7
>>>


W



More information about the Python-list mailing list