I have a mental block, is there a regular python 1-liner to stop comprehension of a list or genex based on some condition? Usage example:
def primes(): # yields forever eg 2, 3, 5, 7, 11, 13 ... [p for p in primes() if p < 10] # will never terminate, and will go onto infinite loop consuming primes() [p for p in primes() while p < 10] # should return [2, 3, 5, 7], and consumed 5 items from my generator
I know about itertools consumes, islice , but these guys require you to know how many items you want to consume in advance.
python generator
wim
source share