Re: [Tutor] Selecting first matching element from a list

2009-01-14 Thread Kent Johnson
On Wed, Jan 14, 2009 at 11:25 AM, bob gailer wrote: > How many ways are there in Python to go thru a list (and apply a function to > each element)? The ones I know are: > while > for > list comprehension > map() filter() is more appropriate than map() for this problem generator expression itertoo

Re: [Tutor] Selecting first matching element from a list

2009-01-14 Thread bob gailer
Noufal Ibrahim wrote: bob gailer wrote: 2) "looks bad" and "is ugly" are emotional responses. What are you feeling and wanting when you say those things? It's hard for me to qualify really but my general idea of list comprehensions is that they transform and filter lists creating other li

Re: [Tutor] Selecting first matching element from a list

2009-01-14 Thread Noufal Ibrahim
bob gailer wrote: 2) "looks bad" and "is ugly" are emotional responses. What are you feeling and wanting when you say those things? It's hard for me to qualify really but my general idea of list comprehensions is that they transform and filter lists creating other lists. For instance, I'd

Re: [Tutor] Selecting first matching element from a list

2009-01-12 Thread Kent Johnson
On Mon, Jan 12, 2009 at 12:21 PM, bob gailer wrote: > Noufal Ibrahim wrote: >> >> Hello everyone, >> What is the pythonic way of selecting the first element of a list >> matching some condition? >> I often end up using >> >> [x for x in lst if cond(x)][0] >> >> This looks b

Re: [Tutor] Selecting first matching element from a list

2009-01-12 Thread wesley chun
> 1) itertools.dropwhile(cond, lst) will return a list with the desired element > first. i think in order to use this, the OP needs to invert his Boolean logic because it *drops* elements while the conditional is True. as soon as it hits False the 1st time, all remaining elements (including the o

Re: [Tutor] Selecting first matching element from a list

2009-01-12 Thread bob gailer
Noufal Ibrahim wrote: Hello everyone, What is the pythonic way of selecting the first element of a list matching some condition? I often end up using [x for x in lst if cond(x)][0] This looks bad and raises IndexError if nothing is found which is ugly too. 1) i

[Tutor] Selecting first matching element from a list

2009-01-12 Thread Noufal Ibrahim
Hello everyone, What is the pythonic was of selecting the first element of a list matching some condition? I often end up using [x for x in lst if cond(x)][0] This looks bad and raises IndexError if nothing is found which is ugly too. Thanks. -- ~noufal http://ni