<p dir="ltr"><br>
On 29 Aug 2013 23:20, "Ben Finney" <<a href="mailto:ben%2Bpython@benfinney.id.au">ben+python@benfinney.id.au</a>> wrote:<br>
><br>
> Fábio Santos <<a href="mailto:fabiosantosart@gmail.com">fabiosantosart@gmail.com</a>> writes:<br>
><br>
> > It is a shame that this is not possible in python. for..if exists in<br>
> > comprehensions and not in regular loops but that would be nice<br>
> > sometimes.<br>
><br>
> So you use it in a generator expression, and iterate over the generator:<br>
><br>
> for foo in (spam for spam in sequence if predicate(spam)):<br>
> process(spam)<br>
><br>
> That way, there's no need for new syntax.</p>
<p dir="ltr">The problem I have with that strategy is that it is repetitive and hinders readability. You wrote "for" and "in" twice, and spam (a pretty useless intermediate variable) thrice! While it does its job, it hides the true intent for filtering beneath a lot of (pun intended) spam. The "if" particle is nigh undetectable there.</p>
<p dir="ltr">To get around this, I often declare a generator. But I still find it a bit awkward to have to look up the definition elsewhere, and to waste lines over something so simple.</p>
<p dir="ltr">I can't say I understand why we don't merge the for loops' syntax with the comprehension syntax. Even after following the for..while discussion.</p>