I have some tuples: (0,1,2), (3,4,4), (2,2,2) and I would like to find the minimum of the the zero index such that the second index equals 2. I have tried this using the min built-in function but it is giving me a syntax error.
min(data, key=lambda x: x if x[2] == 2)
>Solution :
Don’t use the key parameter. If you want to ignore certain elements in finding the minimum, use filter() instead:
data = [(0,1,2), (3,4,4), (2,2,2)]
print(min(filter(lambda x: x[2] == 2, data)))
This outputs:
(0, 1, 2)