How to remove 2nd occurrence of an item in a list using remove() without removing 1st occurrence in Python

It’s not clear to me why this specific task requires a loop:

array = [9, 8, 2, 3, 8, 3, 5]

def remove_2nd_occurance(array, value):

    ''' Raises ValueError if either of the two values aren't present '''

    array.pop(array.index(value, array.index(value) + 1))


remove_2nd_occurance(array, 8)

print(array)

Leave a Comment