Get the first item from an iterable that matches a condition
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Secret Catacombs
--
Chapters
00:00 Get The First Item From An Iterable That Matches A Condition
00:42 Accepted Answer Score 917
02:12 Answer 2 Score 74
02:39 Answer 3 Score 49
03:42 Answer 4 Score 30
05:05 Thank you
--
Full question
https://stackoverflow.com/questions/2361...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #iterator
#avk47
ACCEPTED ANSWER
Score 917
Python 2.6+ and Python 3:
If you want StopIteration to be raised if no matching element is found:
next(x for x in the_iterable if x > 3)
If you want default_value (e.g. None) to be returned instead:
next((x for x in the_iterable if x > 3), default_value)
Note that you need an extra pair of parentheses around the generator expression in this case − they are needed whenever the generator expression isn't the only argument.
I see most answers resolutely ignore the next built-in and so I assume that for some mysterious reason they're 100% focused on versions 2.5 and older -- without mentioning the Python-version issue (but then I don't see that mention in the answers that do mention the next built-in, which is why I thought it necessary to provide an answer myself -- at least the "correct version" issue gets on record this way;-).
Python <= 2.5
The .next() method of iterators immediately raises StopIteration if the iterator immediately finishes -- i.e., for your use case, if no item in the iterable satisfies the condition.  If you don't care (i.e., you know there must be at least one satisfactory item) then just use .next() (best on a genexp, line for the next built-in in Python 2.6 and better).
If you do care, wrapping things in a function as you had first indicated in your Q seems best, and while the function implementation you proposed is just fine, you could alternatively use itertools, a for...: break loop, or a genexp, or a try/except StopIteration as the function's body, as various answers suggested.  There's not much added value in any of these alternatives so I'd go for the starkly-simple version you first proposed.
ANSWER 2
Score 74
Damn Exceptions!
I love Alex Martelli's answer. However, since next() raise a StopIteration exception when there are no items,
i would use the following snippet to avoid an exception:
a = []
item = next((x for x in a), None)
For example,
a = []
item = next(x for x in a)
Will raise a StopIteration exception;
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
ANSWER 3
Score 49
As a reusable, documented and tested function
def first(iterable, condition = lambda x: True):
    """
    Returns the first item in the `iterable` that
    satisfies the `condition`.
    If the condition is not given, returns the first item of
    the iterable.
    Raises `StopIteration` if no item satysfing the condition is found.
    >>> first( (1,2,3), condition=lambda x: x % 2 == 0)
    2
    >>> first(range(3, 100))
    3
    >>> first( () )
    Traceback (most recent call last):
    ...
    StopIteration
    """
    return next(x for x in iterable if condition(x))
Version with default argument
@zorf suggested a version of this function where you can have a predefined return value if the iterable is empty or has no items matching the condition:
def first(iterable, default = None, condition = lambda x: True):
    """
    Returns the first item in the `iterable` that
    satisfies the `condition`.
    If the condition is not given, returns the first item of
    the iterable.
    If the `default` argument is given and the iterable is empty,
    or if it has no items matching the condition, the `default` argument
    is returned if it matches the condition.
    The `default` argument being None is the same as it not being given.
    Raises `StopIteration` if no item satisfying the condition is found
    and default is not given or doesn't satisfy the condition.
    >>> first( (1,2,3), condition=lambda x: x % 2 == 0)
    2
    >>> first(range(3, 100))
    3
    >>> first( () )
    Traceback (most recent call last):
    ...
    StopIteration
    >>> first([], default=1)
    1
    >>> first([], default=1, condition=lambda x: x % 2 == 0)
    Traceback (most recent call last):
    ...
    StopIteration
    >>> first([1,3,5], default=1, condition=lambda x: x % 2 == 0)
    Traceback (most recent call last):
    ...
    StopIteration
    """
    try:
        return next(x for x in iterable if condition(x))
    except StopIteration:
        if default is not None and condition(default):
            return default
        else:
            raise
ANSWER 4
Score 30
The most efficient way in Python 3 are one of the following (using a similar example):
With "comprehension" style:
next(i for i in range(100000000) if i == 1000)
WARNING: The expression works also with Python 2, but in the example is used range that returns an iterable object in Python 3 instead of a list like Python 2 (if you want to construct an iterable in Python 2 use xrange instead).
Note that the expression avoid to construct a list in the comprehension expression next([i for ...]), that would cause to create a list with all the elements before filter the elements, and would cause to process the entire options, instead of stop the iteration once i == 1000.
With "functional" style:
next(filter(lambda i: i == 1000, range(100000000)))
WARNING: This doesn't work in Python 2, even replacing range with xrange due that filter create a list instead of a iterator (inefficient), and the next function only works with iterators.
Default value
As mentioned in other responses, you must add a extra-parameter to the function next if you want to avoid an exception raised when the condition is not fulfilled.
"functional" style:
next(filter(lambda i: i == 1000, range(100000000)), False)
"comprehension" style:
With this style you need to surround the comprehension expression with () to avoid a SyntaxError: Generator expression must be parenthesized if not sole argument:
next((i for i in range(100000000) if i == 1000), False)