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?