Unpacking a tuple set in a list construct (python3)

I would like to use the unpacking on the right side in the assignment:

>>> a = [3,4] >>> b = [1,2,*a] File "<stdin>", line 1 SyntaxError: can use starred expression only as assignment target 

Of course I can do:

 >>> b = [1,2] >>> b.extend(a) >>> b [1, 2, 3, 4] 

But I find it cumbersome. Can I indicate a point? Easy way? Is this planned? Or is there a reason to obviously not have it in the language?

Part of the problem is that all container types use a constructor that expects iterability and does not accept the * args argument. I could have subclassed, but this represented some non-pythonic noise for scripts that others had to read.

+4
source share
4 answers

This is fixed in Python 3.5, as described in PEP 448 :

 >>> a=[3,4] >>> b=[1,2,*a] >>> b [1, 2, 3, 4] 
+1
source

You can use the add statement:

 a = [3, 4] b = [1, 2] + a 
+7
source

You have several options, but it is best to use list concatenation ( + ):

 b = [1,2] + a 

If you really want to use * syntax, you can create your own list wrapper:

 def my_list(*args): return list(args) 

then you can name it as:

 a = 3,4 b = my_list(1,2,*a) 

I assume that the advantage here is that a does not have to be a list, it can be any type of sequence.

+5
source

No, this is not planned. The list of arbitrary parameters *arg (and **kw matching keyword arguments) is applicable only to python call calls ( *arg and **kw function signatures are mirrored), and the iterative assignment on the left.

You can simply combine the two lists:

 b = [10, 2] + a 
+2
source

Source: https://habr.com/ru/post/1435041/


All Articles