Skip to content

Instantly share code, notes, and snippets.

@amcgregor
Forked from sirpengi/gist:2898083
Created June 8, 2012 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save amcgregor/2898119 to your computer and use it in GitHub Desktop.
Save amcgregor/2898119 to your computer and use it in GitHub Desktop.
def one(iterable, condition=bool):
found = False
for i in iterable:
if not condition(i): continue
if found: return False
found = True
return found
def first(iterable, condition=lambda a: True, sentinel=None):
try:
return (i for i in iterable if condition(i)).next()
except StopIteration:
return sentinel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment