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

前端 未结 8 621
清歌不尽
清歌不尽 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条回答
  •  -上瘾入骨i
    2020-12-13 13:18

    Mabe this, but it's more or less the same verbose...

    import itertools
    
    for airport in itertools.ifilter(lambda x: x.is_important, airports):
        ...
    

提交回复
热议问题