Yield multiple values

Python

Python Problem Overview


Can't we yield more than one value in the python generator functions?

Example,

In [677]: def gen():
   .....:     for i in range(5):
   .....:         yield i, i+1
   .....:         

In [680]: k1, k2 = gen()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-680-b21f6543a7e9> in <module>()
----> 1 k1, k2 = a()

ValueError: too many values to unpack

This works as follows:

In [678]: b = a()

In [679]: list(b)
Out[679]: [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]

Same results even when I do this:

In [692]: def a():
    for i in range(5):
        yield i
        yield i+1

Thanks.

Python Solutions


Solution 1 - Python

Because gen() returns a generator (a single item - so it can't be unpacked as two), it needs to be advanced first to get the values...

g = gen()
a, b = next(g)

It works with list because that implicitly consumes the generator.

Can we further make this a generator? Something like this:

g = gen();
def yield_g():
    yield g.next();
    k1,k2 = yield_g();
 

and therefore list(k1) would give [0,1,2,3,4] and list(k2) would give [1,2,3,4,5].

Keep your existing generator, and use izip (or zip):

from itertools import izip
k1, k2 = izip(*gen())

Solution 2 - Python

Your function gen returns a generator and not values as you might expect judging from the example you gave. If you iterate over the generator the pairs of values will be yielded:

In [2]: def gen():
   ...:     for i in range(5):
   ...:         yield i, i+1
   ...:         

In [3]: for k1, k2 in gen():
   ...:     print k1, k2
   ...:     
0 1
1 2
2 3
3 4
4 5

Solution 3 - Python

Use yield from

def gen():
    for i in range(5):
        yield from (i, i+1)

[v for v in gen()]
# [0, 1, 1, 2, 2, 3, 3, 4, 4, 5]

The python docs say:

> When yield from <expr> is used, it treats the supplied expression as a subiterator.

Solution 4 - Python

Both Solution works just fine, in case you want to return multiple values from the generator.

 def gen():
    for i in range(5):
        yield i, i+1

Solution 1:

 for values in gen():
     print(values[0],values[1])

Solution 2:

 for value1, value2 in gen():
     print(value1, value2)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShyam SunderView Question on Stackoverflow
Solution 1 - PythonJon ClementsView Answer on Stackoverflow
Solution 2 - PythonDavid ZwickerView Answer on Stackoverflow
Solution 3 - PythonforzagreenView Answer on Stackoverflow
Solution 4 - PythonArpan SainiView Answer on Stackoverflow