1

Is it possible to get multiple return values from inline for? e.g.,:

a,b=[(1,2) for _ in range(3)]

such that:

a=[1,1,1]
b=[2,2,2]
jsbueno
  • 99,910
  • 10
  • 151
  • 209
Mercury
  • 1,886
  • 5
  • 25
  • 44
  • 2
    I would use `a, b = [1] * 3, [2] * 3` or `a, b = ([i] * 3 for i in (1, 2))` *(if you want to apply generator)*. – Olvin Roght Sep 17 '20 at 20:21
  • Does this answer your question? [Transpose/Unzip Function (inverse of zip)?](https://stackoverflow.com/questions/19339/transpose-unzip-function-inverse-of-zip) – yao99 Sep 17 '20 at 21:34

2 Answers2

8

You can use the zip function

>>> a, b = zip(*[(1,2) for _ in range(3)])
>>> a
(1, 1, 1)
>>> b
(2, 2, 2)

or also

>>> a, b = [1]*3, [2]*3
>>> a
[1, 1, 1]
>>> b
[2, 2, 2]
>>> 
abc
  • 11,579
  • 2
  • 26
  • 51
2

The zip built-in can be kind of "abused" to yield this kind of output:

In [203]: a, b = zip(*((1,2) for _ in range(3)))                                                                       

In [204]: a                                                                                                            
Out[204]: (1, 1, 1)

In [205]: b                                                                                                            
Out[205]: (2, 2, 2)
jsbueno
  • 99,910
  • 10
  • 151
  • 209