469,626 Members | 1,518 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

Find the first element that meets the condition

Hi,
I have a list and I want to find the first element that meets a
condition. I do not want to use 'filter', because I want to come out
of the iteration as soon as the first element is found.
I have implemented it this way, may be, there should be a built in
hiding somewhere in the standard libraries?

def exists(iterable, condition):
'''
Return the first element in iterble that meets the condition.
'''
for x in iterable:
if condition(x):
return x
raise Exception('No element meets the given condition.')
>>exists(xrange(1000), lambda x: x>13)
14

-
Suresh

Feb 25 '07 #1
3 5486
I have implemented it this way, may be, there should be a built in
hiding somewhere in the standard libraries?
the itertools module might have what you're after. something similar
to your example:
>>import itertools
r = iter(range(100))
n = itertools.dropwhile(lambda x:x<=13, r)
n.next()
14
Feb 25 '07 #2
jm*******@no.spam.gmail.com wrote:
I have a list and I want to find the first element that meets a
condition. I do not want to use 'filter', because I want to come out
of the iteration as soon as the first element is found.
I have implemented it this way, may be, there should be a built in
hiding somewhere in the standard libraries?

def exists(iterable, condition):
'''
Return the first element in iterble that meets the condition.
'''
for x in iterable:
if condition(x):
return x
raise Exception('No element meets the given condition.')
>>>exists(xrange(1000), lambda x: x>13)
14
If you are only interested in existence you can use any() (new in Python2.5)
>>any(x>13 for x in xrange(1000))
True

Otherwise there is itertools.ifilter() a lazy variant of the filter()
builtin:
>>import itertools
items = iter(xrange(1000)) # just to prove...
itertools.ifilter(lambda x: x 13, items).next()
14
>>items.next() # that ifilter has consumed only the first 15 items
15

You may want to wrap the ifilter() call to get a more sensible exception,
say ValueError instead of StopIteration:

# untested
def findfirst(items, predicate=bool):
for item in itertools.ifilter(predicate, items):
return item
raise ValueError("No matching element")

Peter

Feb 25 '07 #3
"jm*******@no.spam.gmail.com" <jm*******@gmail.comwrites:
I have a list and I want to find the first element that meets a
condition. I do not want to use 'filter', because I want to come out
of the iteration as soon as the first element is found.
I have implemented it this way, may be, there should be a built in
hiding somewhere in the standard libraries?

def exists(iterable, condition):
To check for existence, use any(condition, iterable). If you actually
want the first element, use itertools.ifilter(condition, iterable).next().
The suggestion of using dropwhile isn't so great because it actually
consumes the first match, so you have to write your condition to
stop immediately before the element you want, not always easy.
Feb 25 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Neal D. Becker | last post: by
8 posts views Thread by Rom | last post: by
8 posts views Thread by Jim Langston | last post: by
5 posts views Thread by Draw | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.