0

I'm super new to programming. I'm supposed to find the runner up score. My logic is sorting the list, deleting the maximum element(s) and printing the new maximum element.

Here's what I've tried:

enter code here

n = int(input())

arr = list(map(int , input().split()))
arr.sort()
a=arr
for i in range(len(a)-1):
 if a[i]==max(arr):
  del a[i]
 else:
  continue
print(arr)

When I give n=5 and list elements [2,3,5,6,6] then my output is [2,3,5,6]. The other 6 isn't getting removed. I suspect that del is messing with the indexing of the list. Please do tell me where I've gone wrong, thank you.

Grismar
  • 27,561
  • 4
  • 31
  • 54
  • 1
    Read the duplicate question. Also note that `a = arr` will **not** create a new copy of `arr`, so when you `del a[i]` you are actually deleting from `arr` as well, causing the value of `max(arr)` to change. – Selcuk Nov 15 '21 at 03:22
  • you don't need `else: continue` if you don't have other code inside for-loop after `else:continue` – furas Nov 15 '21 at 14:41
  • deleting data in list which you iterate is not good idea. and this is why it doesn't delete other number - after deleting `a[4]` it automatically move `a[5]` in place of `a[4]` and then it skip this value. In Python better move to new list elements which you want to keep – furas Nov 15 '21 at 14:42

0 Answers0