pythonpython-3.xlistlist-manipulation

Why my python program returning different Output on every execution


I have been doing a challenge to find a runner up score from a list
or
You can say I was trying to find the second largest from a list.

array = '57 57 -57 57'

print(list(set(sorted(array.split(' '))))[-2])

But on every execution the program returning different Output.

Why it is behaving like this??


Solution

  • Change it to this:

    array = '57 57 -57 57'
    
    print(list(sorted(set(array.split(' '))))[-2])
    

    'set' doesn't preserve the order, so first get the set value and then sort it.