wheres pythonmonks wrote:
This should be trivial:
I am looking to extract the first non-None element in a list, and
"None" otherwise. Here's one implementation:
x = reduce(lambda x,y: x or y, [None,None,1,None,2,None], None)
print x
1
I thought maybe a generator expression would be better,
On Sep 2, 2:48 pm, wheres pythonmonks
wrote:
> This should be trivial:
>
> I am looking to extract the first non-None element in a list, and
> "None" otherwise. Here's one implementation:
>
> >>> x = reduce(lambda x,y: x or y, [None,None,1,None,2,None], None)
> >>> print x
>
> 1
>
> I thought may
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 fo
wheres pythonmonks wrote:
> I am looking to extract the first non-None element in a list, and
> "None" otherwise. Here's one implementation:
>
x = reduce(lambda x,y: x or y, [None,None,1,None,2,None], None)
print x
> 1
>
> I thought maybe a generator expression would be better, to pre
This should be trivial:
I am looking to extract the first non-None element in a list, and
"None" otherwise. Here's one implementation:
>>> x = reduce(lambda x,y: x or y, [None,None,1,None,2,None], None)
>>> print x
1
I thought maybe a generator expression would be better, to prevent
iterating