I tried this in Python, thinking it would give me [(1,123),(2,123)]:
Python 2.7.3 (default, Feb 27 2014, 19:58:35)
>>> def my_generator():
... yield 123
...
>>> zip([1,2], my_generator())
[(1, 123)]
Why does zip stop after creating just one item? Is there a Pythonic way to get what I was looking for?
Create an infinite generator, like this
def my_generator():
while True:
yield 123
print zip([1,2], my_generator())
# [(1, 123), (2, 123)]
The better way to do this would be, using itertools.repeat
, like this
from itertools import repeat
print zip([1,2], repeat(123))
# [(1, 123), (2, 123)]
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