Get the first item from an iterable that matches a condition
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 … Read more