Re: first non-null element in a list, otherwise None

2010-09-02 Thread Gerard Flanagan
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,

Re: first non-null element in a list, otherwise None

2010-09-02 Thread Arnaud Delobelle
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

Re: first non-null element in a list, otherwise None

2010-09-02 Thread wheres pythonmonks
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

Re: first non-null element in a list, otherwise None

2010-09-02 Thread Peter Otten
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

first non-null element in a list, otherwise None

2010-09-02 Thread wheres pythonmonks
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