user1747134 user1747134 - 1 year ago 134
Python Question

Evaluate iterator in python

I have a lazy iterator/generator

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 Source

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

collections.deque(iterator, maxlen=0)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download