Created
October 17, 2019 14:30
-
-
Save cupdike/0832e29c0121bc9d71f16ad98c58355b to your computer and use it in GitHub Desktop.
Combine Python Generators Into One Generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 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