understanding “item for item in list_a if …” PYTHON

后端 未结 4 1353
北海茫月
北海茫月 2021-02-04 21:48

I\'ve seen the following code many times, and I know it\'s the solution to my problems, but I\'m really struggling to understand HOW it works. The code in particular is:

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-04 22:37

    The list comprehension [item for item in list_a if item not in list_b] is equivalent to using the filter function:

    filter(list_a, lambda item: item not in list_b)
    

提交回复
热议问题