Skip to content

Instantly share code, notes, and snippets.

@cupdike
Created October 17, 2019 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cupdike/0832e29c0121bc9d71f16ad98c58355b to your computer and use it in GitHub Desktop.
Save cupdike/0832e29c0121bc9d71f16ad98c58355b to your computer and use it in GitHub Desktop.
Combine Python Generators Into One Generator
>>> def genX():
... for i in range(3):
... yield i
...
>>> for i in genX(): print(i)
...
0
1
2
>>> def genY():
... for i in range(3):
... yield i*10
...
>>> for i in genY(): print(i)
...
0
10
20
>>> def genCombined():
... for genBoth in [genX(), genY()]:
... yield from genBoth
...
>>> for i in genCombined(): print(i)
...
0
1
2
0
10
20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment