Single line for loop over iterator with an "if" filter?
loops, python
Solution
No, there is no shorter way. Usually, you will even break it into two lines :
important_airports = (airport for airport in airports if airport.is_important)
for airport in important_airports:
# do stuff
This is more flexible, easier to read and still don't consume much memory.
Problem
I have a simple `for` loop followed by a simple `if` statement: ``` for airport in airports: if airport.is_important: ``` and I was wondering if I can write this as a single line somehow. Yes, I can do this: ``` for airport in (airport for airport in airports if airport.is_important): ``` but it reads so silly and redundant (`for airport in airport for airport in airports...`). Is there a better way?