I have two generators say A()
and B()
. I want to iterate over both the generators together. Something like:
for a,b in A(),B(): # I know this is wrong #do processing on a and b
One way is to store the results of both the functions in lists and then loop over the merged list. Something like this:
resA = [a for a in A()] resB = [b for b in B()] for a,b in zip(resA, resB): #do stuff
If you are wondering, then yes both the functions yield equal number of value.
But I can't use this approach because A()/B()
returns so many values. Storing them in a list would exhaust the memory, that's why I am using generators.
Is there any way to loop over both the generators at once?
This is because generators, like all iterators, can be exhausted. Unless your generator is infinite, you can iterate through it one time only. Once all values have been evaluated, iteration will stop and the for loop will exit.
A generator is a kind of iterator. An iterator is a kind of iterable object, and like any other iterable, You can iterate over every item using a for loop: for vote in Vote.objects.get_top(myModel, limit=10, reversed=False): print v.name, vote.
It is used to abstract a container of data to make it behave like an iterable object. Some common iterable objects in Python are - lists, strings, dictionary. Every generator is an iterator, but not vice versa. A generator is built by calling a function that has one or more yield expressions.
You were almost there. In Python 3, just pass the generators to zip()
:
for a, b in zip(A(), B()):
zip()
takes any iterable, not just lists. It will consume the generators one by one.
In Python 2, use itertools.izip()
:
from itertools import izip for a, b in izip(A(), B()):
As an aside, turning a generator into a list is as simple as list(generator)
; no need to use a list comprehension there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With