This is not recommended, but...
>>> def notyet(predicate):
if predicate:
raise StopIteration
return True
>>> # note that below we're wrapping a generator expression in a
>>> # call to `list` so that when `StopIteration` is raised your
>>> # program won't terminate.
>>> xs = list((i, r) for (i, r) in foo() if notyet(i!=0))
>>> # if we try this instead, your program will hit the buffers
>>> # when the predicate is satisfied.
>>> xs = [(i, r) for (i, r) in foo() if notyet(i!=0)]
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
xs = [(i, r) for (i, r) in foo() if notyet(i!=0)]
File "<pyshell#3>", line 3, in notyet
raise StopIteration
StopIteration
It's much better to use itertools.takewhile in this situation.
>>> from itertools import takewhile
>>> xs = list(takewhile(lambda (i, r): i!=0, foo()))