Does anybody know the reasoning as to why the unary (*
) operator cannot be used in an expression involving iterators/lists/tuples?
Why is it only limited to function unpacking? or am I wrong in thinking that?
For example:
>>> [1,2,3, *[4,5,6]]
File "<stdin>", line 1
[1,2,3, *[4,5,6]]
^
SyntaxError: invalid syntax
Why doesn't the *
operator:
[1, 2, 3, *[4, 5, 6]] give [1, 2, 3, 4, 5, 6]
whereas when the *
operator is used with a function call it does expand:
f(*[4, 5, 6]) is equivalent to f(4, 5, 6)
There is a similarity between the +
and the *
when using lists but not when extending a list with another type.
For example:
# This works
gen = (x for x in range(10))
def hello(*args):
print args
hello(*gen)
# but this does not work
[] + gen
TypeError: can only concatenate list (not "generator") to list