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

前端 未结 3 826
余生分开走
余生分开走 2020-12-04 03:09
a = [9,8,2,3,8,3,5]

How to remove 2nd occurrence of 8 without removing 1st occurrence of 8 using remove().

3条回答
  •  一个人的身影
    2020-12-04 03:56

    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)
    

提交回复
热议问题