Remove items from a list while iterating without using extra memory in Python

后端 未结 8 952
生来不讨喜
生来不讨喜 2020-12-10 05:45

My problem is simple: I have a long list of elements that I want to iterate through and check every element against a condition. Depending on the outcome of the condition I

8条回答
  •  孤街浪徒
    2020-12-10 06:12

    li = [ x for x in li if condition(x)]
    

    and also

    li = filter(condition,li) 
    

    Thanks to Dave Kirby

提交回复
热议问题