Sobes.tech
Junior — Senior

Improving Search Function Efficiency

livecode

Task condition

It is necessary to suggest several options for improving the get_first_matching_object function. Consider using generators, Python's built-in functions, and safe methods for working with default values.

def get_first_matching_object(cond, items=[]):
    found = [obj for obj in items if cond(obj)]
    if found:
        result = found[0]
        return result
    return None

get_first_matching_object(lambda x: x == 1, [2, 3, 4])