Give lists a, b
a = [5, 8, 9]
b = [6, 1, 0]
I want to create a generator gen such that:
for x in gen:
print x
outputs
5, 8, 9, 6, 1, 0
You could use itertools.chain
:
>>> from itertools import chain
>>> a = [5, 8, 9]
>>> b = [6, 1, 0]
>>> it=chain(a,b)
>>> for x in it:
print x,
...
5 8 9 6 1 0
def chain(*args):
for arg in args:
for item in arg:
yield item
a = [5, 8, 9]
b = [6, 1, 0]
for x in chain(a,b):
print x,
print ', '.join(map(str,chain(a,b)))
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