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

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 … Read more

What do ** (double star/asterisk) and * (star/asterisk) mean in a function call?

A single star * unpacks a sequence or collection into positional arguments. Suppose we have def add(a, b): return a + b values = (1, 2) Using the * unpacking operator, we can write s = add(*values), which will be equivalent to writing s = add(1, 2). The double star ** does the same thing … Read more

Unpacking, extended unpacking and nested extended unpacking

My apologies for the length of this post, but I decided to opt for completeness. Once you know a few basic rules, it’s not hard to generalize them. I’ll do my best to explain with a few examples. Since you’re talking about evaluating these “by hand,” I’ll suggest some simple substitution rules. Basically, you might … Read more

What does the star and doublestar operator mean in a function call?

The single star * unpacks the sequence/collection into positional arguments, so you can do this: def sum(a, b): return a + b values = (1, 2) s = sum(*values) This will unpack the tuple so that it actually executes as: s = sum(1, 2) The double star ** does the same, only using a dictionary … Read more

What does ** (double star/asterisk) and * (star/asterisk) do for parameters?

The *args and **kwargs is a common idiom to allow arbitrary number of arguments to functions as described in the section more on defining functions in the Python documentation. The *args will give you all function parameters as a tuple: def foo(*args): for a in args: print(a) foo(1) # 1 foo(1,2,3) # 1 # 2 … Read more