3 ms·
Implementing a find function makes this read a lot nicer. You could also call it find_first or find_next. def find(predicate, iterable, default=None):
by tommikaikkonen 10y ago
Implementing a find function makes this read a lot nicer. You could also call it find_first or find_next.
def find(predicate, iterable, default=None):
"""Returns the first value that matches predicate, otherwise default=None"""
return next(
(x for x in iterable if predicate(x)),
default
)
Which turns the expression to
find(lambda x: 'widgets' in x, mixed_widgets)['widgets']