[Solved] Python maximum and minimum


If you want to find max/min via traversal/iteration – use the following approach:

def max_and_min(values):
    max_v = min_v = values[0]
    for v in values[1:]:
        if v < min_v:
            min_v = v
        elif v > max_v:
            max_v = v

    return (max_v, min_v)

l = [1,10,2,3,33]
print(max_and_min(l))

The output:

(33, 1)

1

solved Python maximum and minimum