Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Splat/unpack operator * in python cannot be used in an expression?

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 
like image 205
Har Avatar asked Jan 13 '16 12:01

Har


People also ask

How do you unpack an operator in Python?

The * operator is an unpacking operator that will unpack the values from any iterable object, such as lists, tuples, strings, etc… And that's it! The asterisk, *, or unpacking operator, unpacks num_list, and passes the values, or elements, of num_list as separate arguments to the num_sum function.

Can you unpack a dictionary in Python?

Using the Unpack Operator ( ** ) to Unpack Dictionary in Python. The ** operator is used to pack and unpack dictionary in Python and is useful for sending them to a function.

What is the unpacking operator in Python?

Sure there is, and it’s called unpacking operator or asterisk operator (*, **). Let’s see how to use it in Python. The asterisk operator (*) is used to unpack all the values of an iterable that have not been assigned yet.

How to unpack arguments from a list or tuple in Python?

As another example, consider the built-in range () function that expects separate start and stops arguments. If they are not available separately, write the function call with the *-operator to unpack the arguments out of a list or tuple:

How do I unpack a dictionary in Python?

To unpack a dictionary, we need to use the ** unpacking operator. However, since each value is associated with a specific key, the function that we pass these arguments to must have parameters with the same names as the keys of the dictionary being unpacked. For example: This dict_sum function has three parameters: a, b, and c.

What is the use of * operator in Python?

The * operator is an unpacking operator that will unpack the values from any iterable object, such as lists, tuples, strings, etc… For example, if we want to unpack num_list and pass in the 5 elements as separate arguments for the num_sum function, we could do so as follows: And that’s it!


2 Answers

Unpacking in list, dict, set, and tuple literals has been added in Python 3.5, as described in PEP 448:

Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:27:37) on Windows (64 bits).  >>> [1, 2, 3, *[4, 5, 6]] [1, 2, 3, 4, 5, 6] 

Here are some explanations for the rationale behind this change. Note that this does not make *[1, 2, 3] equivalent to 1, 2, 3 in all contexts. Python's syntax is not intended to work that way.

like image 195
B. M. Avatar answered Sep 25 '22 21:09

B. M.


Asterix * isn't simply unary operator, it's argument-unpacking operator for functions definitions and functions calls.

So * supposed to be used only to work with function params and not with lists, tuples etc.

NOTE: starting from python3.5, * could be used not only with functions params, @B. M's answer greatly describes that change in python.

If you need to concat lists use concatenation instead list1 + list2 to get desired result. To concatenate list and generator simply pass generator to list type object, prior concatenating with another list:

gen = (x for x in range(10)) [] + list(gen) 
like image 24
Andriy Ivaneyko Avatar answered Sep 23 '22 21:09

Andriy Ivaneyko