Remove all occurrences of a value from a list?

后端 未结 23 1959
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 23:45

In Python remove() will remove the first occurrence of value in a list.

How to remove all occurrences of a value from a list?

This is w

23条回答
  •  心在旅途
    2020-11-21 23:51

    To remove all duplicate occurrences and leave one in the list:

    test = [1, 1, 2, 3]
    
    newlist = list(set(test))
    
    print newlist
    
    [1, 2, 3]
    

    Here is the function I've used for Project Euler:

    def removeOccurrences(e):
      return list(set(e))
    

提交回复
热议问题