Remove all occurrences of a value from a list?

后端 未结 23 1862
佛祖请我去吃肉
佛祖请我去吃肉 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:52

    I just did this for a list. I am just a beginner. A slightly more advanced programmer can surely write a function like this.

    for i in range(len(spam)):
        spam.remove('cat')
        if 'cat' not in spam:
             print('All instances of ' + 'cat ' + 'have been removed')
             break
    

提交回复
热议问题