-1

when i compiled this program in VSCode, I got IndexError. Is there any other solution?

#here is my sample code
if __name__ == '__main__':
    n = int(input())
    arr = list(map(int, input().split()))
    x = len(arr)
    arr.sort()
    for i in range(0, x-1):
        #removing redundant values
        if arr[i] == max(arr):
            arr.remove(arr[i])

    arr.remove(max(arr))
    print(max(arr))
Brett Cannon
  • 14,438
  • 3
  • 45
  • 40
  • Possible duplicate of [How to remove items from a list while iterating?](https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating) – Rahul May 31 '19 at 11:40
  • 3
    Possible duplicate of [Get the second largest number in a list in linear time](https://stackoverflow.com/questions/16225677/get-the-second-largest-number-in-a-list-in-linear-time) – Christian Sloper May 31 '19 at 11:43

2 Answers2

0

split using split(',') when input is 6,6,6,6,6,6,6,6,6,5

arr = list(map(int, input().split(',')))

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
  • If you look closely OP implements `.remove()` while iterating over the array, which explains the IndexError – Rahul May 31 '19 at 11:51
  • the split function is working perfectly fine, the error is in the for loop. – Yash Patel May 31 '19 at 11:54
  • @Rahul You're right. What to do in order to overcome this issue? – Yash Patel May 31 '19 at 11:56
  • @YashPatel I've linked an answer in the comments, here you go again https://stackoverflow.com/questions/1207406/how-to-remove-items-from-a-list-while-iterating – Rahul May 31 '19 at 11:57
  • @YashPatel wont `sorted(array)[-2]` is you solution, then why writing for loop – sahasrara62 May 31 '19 at 11:59
  • @Rahul using split in this manner itself is giving error plus his logic is wrong – sahasrara62 May 31 '19 at 12:04
  • @prashantrana I don't think OP meant to actually give input as `6,6,6,6,6,6,6,6,6,5`, it was just the way OP typed it into the question, yes his logic is wrong too. This has 2 questions in itself due to ambiguity. – Rahul May 31 '19 at 12:07
  • @Rahul he mentioned that input is given this way, read question – sahasrara62 May 31 '19 at 12:08
  • @prashantrana That is what I'm trying to say, by guessing his program I don't think OP meant to give input that way, I'm just saying he typed it that way into the question, as I said it has 2 questions in itself due to ambiguity. Since he lists IndexError and not something else. – Rahul May 31 '19 at 12:09
  • @prashantrana The program is working for other inputs. But when there are more similar values, the loop removes those elements and IndexError occurs. Can you guys give me a solution regarding my issue? Thanks. – Yash Patel Jun 02 '19 at 10:04
0

Seeing as you're not updating the length after the first iteration you will end up, thinking you have a bigger array than you do, as you remove them all. Resulting in an IndexError.

If you make a copy of the array you can always iterate over that allowing you to edit the original list meanwhile.