Single line for loop over iterator with an “if” filter?

前端 未结 8 624
清歌不尽
清歌不尽 2020-12-13 12:55

Silly question:
I have a simple for loop followed by a simple if statement:

for airport in airports:
    if airport.is_important:

and

8条回答
  •  遥遥无期
    2020-12-13 13:09

    You could do

    for airport in filter(lamdba x: x.is_important, airports):
        # do stuff...
    

提交回复
热议问题