Write a Python function that returns the values of the largest and second largest elements in the passed list.


Topic: Write a Python function that returns the values of the largest and second largest elements in the passed list.

Solution

def max2(x):
    if x[0] > x[1]:
        m1,m2 = (x[0],x[1])
    else:
        m1,m2 = (x[1],x[0])
    for i in range(2, len(x)):
        if x[i] > m1:
            m2 = m1
            m1 = x[i]
        elif x[i] > m2:
            m2 = x[i]
    return m1,m2



List all Python Programs