Sobes.tech
Junior — Senior

Find the first matching element with the walrus operator (:=)

livecode

Task condition

It is necessary to analyze the operation of the function get_first_matching_object, where the assignment operator := is used inside the loop. Determine what value will be returned if the function is called with the array [1, 2, 3, 4] and the predicate lambda x: x == 1.

def get_first_matching_object(predicate, objects=[]):
    for obj in objects:
        if object := predicate(obj):
            return object
    return None

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