87
votes

How can one loop through a generator? I thought about this way:

gen = function_that_returns_a_generator(param1, param2)
if gen: # in case the generator is null
    while True:
        try:
            print gen.next()
        except StopIteration:
            break

Is there a more pythonic way?

7
I would suggest using break; not continue - Jon Clements♦
I would actually do it this way in the case where the generator may throw an exception on an element, but you don't want to stop the iteration. - robbrit
I would like to measure the execution time of each generator invocation. What's a reasonably elegant & pythonic way to structure a loop that can get the timestamp before and after each invocation? - chrisinmtown

7 Answers

152
votes

Simply

for x in gen:
    # whatever

will do the trick. Note that if gen always returns True.

17
votes
for item in function_that_returns_a_generator(param1, param2):
    print item

You don't need to worry about the test to see if there is anything being returned by your function as if there's nothing returned you won't enter the loop.

10
votes

In case you don't need the output of the generator because you care only about its side effects, you can use the following one-liner:

for _ in gen: pass
7
votes

You can simply loop through it:

>>> gen = (i for i in range(1, 4))
>>> for i in gen: print i
1
2
3

But be aware, that you can only loop one time. Next time generator will be empty:

>>> for i in gen: print i
>>> 
4
votes

Just treat it like any other iterable:

for val in function_that_returns_a_generator(p1, p2):
    print val

Note that if gen: will always be True, so it's a false test

2
votes

If you want to manually move through the generator (i.e., to work with each loop manually) then you could do something like this:

    from pdb import set_trace

    for x in gen:
        set_trace()
        #do whatever you want with x at the command prompt
        #use pdb commands to step through each loop of the generator e.g., >>c #continue   
2
votes

The other answers are good for complicated scenarios. If you simply want to stream the items into a list:

x = list(generator)

(or, if you just want to trigger the generator to do stuff, simply list(generator).

For simple preprocessing, use list comprehensions:

x = [tup[0] for tup in generator]

Or when you want to execute simple functions:

# didn't assign to variable b/c we don't care about what the print() function returns
[print(x) for x in gen]