I have a function that yields 2 parts of a list:
>>> x = [1,2,3,4,5,6]
>>> def fold(ls):
... for i in range(0,2):
... yield x[:i]
... yield x[i:]
...
I understand that it still returns as 1 generator although there're more than 1 yield in the function:
>>> fold(x)
<generator object fold at 0x13f9320>
>>> t1, t2 = fold(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
To access them, I have could:
>>> for i in fold(x):
... print i
...
[]
[1, 2, 3, 4, 5, 6]
[1]
[2, 3, 4, 5, 6]
I want to perform different operation on these two, so i would need some sort of access like this:
>>> for i in fold(x):
... t1, t2 = i
... print t1, t2 # because
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ValueError: need more than 0 values to unpack
I could use some tricks on the odd/even-ness of the yields and do the following but is there any other way?
>>> for i,j in enumerate(fold(x)):
... if i & 0x1:
... print 't1', j
... else:
... print 't2', j
...
t2 []
t1 [1, 2, 3, 4, 5, 6]
t2 [1]
t1 [2, 3, 4, 5, 6]