user1747134 user1747134 - 14 days ago 7
Python Question

Evaluate iterator in python

I have a lazy iterator/generator

a
which is doing some side-effect on each iteration and I don't care about the values that it produces. What is the most idiomatic way to evaluate the side effects? Of course, I can simply do:

for _ in a: pass


But it seems rather ugly.

Answer

I found the recipe for the function consume in itertools recipes section. Esentially, it recommends to do:

collections.deque(iterator, maxlen=0)