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

前端 未结 8 620
清歌不尽
清歌不尽 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:05

    I'd use a negative guard on the loop. It's readable, and doesn't introduce an extra level of indentation.

    for airport in airports:
        if not airport.is_important: continue
        
    

提交回复
热议问题