You need to both filter and use the key argument for max:
from operator import itemgetter max(filter(lambda a: a[2] >= 100, yourlist), key=itemgetter(1))
A filter can also be expressed as a generator expression:
max((t for t in yourlist if t[2] >= 100), key=itemgetter(1))
Demo:
>>> yourlist = [(1, 2, 300), (2, 3, 400), (3, 6, 50)] >>> max((t for t in yourlist if t[2] >= 100), key=itemgetter(1)) (2, 3, 400) >>> max(filter(lambda a: a[2] >= 100, yourlist), key=itemgetter(1)) (2, 3, 400)
Note that since you are filtering, it is easy to get an empty list to select max from, so you might need to catch a ValueError if you don't need this exception to propagate the call stack:
try: return max(filter(lambda a: a[2] >= 100, yourlist), key=itemgetter(1)) except ValueError:
source share