how to correctly modify the iterator of a loop in python from within the loop

后端 未结 4 1634
清酒与你
清酒与你 2021-02-08 11:37

what I basically need is to check every element of a list and if some criteria fit I want to remove it from the list.

So for example let\'s say that

list         


        
4条回答
  •  没有蜡笔的小新
    2021-02-08 12:16

    If you are ok with creating a copy of the list you can do it like this (list comprehension):

    [s for s in list if s != 'b' and s != 'c']
    

提交回复
热议问题