I was looking for an elegant (short!) way to return the first element of a list that matches a certain criteria without necessarily having to evaluate the criteria for every element of the list. Eventually I came up with:
(e for e in mylist if my_criteria(e)).next()
Is there a better way to do it?
To be more precise: There's built in python functions such as all()
and any()
- wouldn't it make sense to have something like first()
too? For some reason I dislike the call to next()
in my solution.