Search code examples
pythoniteratorgenerator

combine infinite with finite iterator


I have two nested loops, the outer one using an infinite generator, and the inner a finite iterator (a list of numbers):

for j in itertools.count():
   for q in Q:
       ...

Under a given condition, I need to break out of both loops, which makes the code inelegant (I have to set a flag to trigger the second break). To avoid that, I thought I'd combine the two loops into one using

for j, q in itertools.product(itertools.count(), Q):
    ...

But when I did that, my computer got slower and slower, apparently swapping memory, until I had to kill the Python process.

The documentation of product says

the actual implementation does not build up intermediate results in memory

so I assumed the result of product to be a generator, which creates elements of the product on demand. But the slowing down and swapping speaks against that.

What did I wrong? How can I achieve what I wanted, a single loop combining an infinite generator with a list?


Solution

  • You can create a generator using a comprehension. Comprehensions support iterating over multiple iterables at once using multiple fors

    for j, q in ((j, q) for j in itertools.count() for q in Q):
        ...