72

As mentioned in PythonCookbook, * can be added before a tuple. What does * mean here?

Chapter 1.18. Mapping Names to Sequence Elements:

from collections import namedtuple
Stock = namedtuple('Stock', ['name', 'shares', 'price'])
s = Stock(*rec) 
# here rec is an ordinary tuple, for example: rec = ('ACME', 100, 123.45)

In the same section, **dict presents:

from collections import namedtuple
Stock = namedtuple('Stock', ['name', 'shares', 'price', 'date', 'time'])
# Create a prototype instance
stock_prototype = Stock('', 0, 0.0, None, None)
# Function to convert a dictionary to a Stock
def dict_to_stock(s):
    return stock_prototype._replace(**s)

What is **'s function here?

3
  • 2
    Please read Python tutorial. (Unpacking Argument Lists) Commented Feb 16, 2014 at 8:51
  • 3
    @MartijnPieters This is not a duplicate question if you starting with the language. For me this question gave a different perspective then *arg and **kwargs. Commented Jul 3, 2018 at 0:24
  • 1
    @AnuragJain: How is it different? They both ask what * and ** mean in the context of a function call. Being a duplicate is not merely asking a slightly different question, it has to be a question that would not lead to effectively identical answers. In both questions, the answer needed explains the same things. The answer here adds information not requested by the person who asked the question, which is fine, but the information requested here is a subset of what the other question requested. This is a great breadcrumb, but it's still a duplicate. Commented Jan 31, 2023 at 19:29

1 Answer 1

194

In a function call

*t means "treat the elements of this iterable as positional arguments to this function call."

def foo(x, y):
    print(x, y)

>>> t = (1, 2)
>>> foo(*t)
1 2

Since v3.5, you can also do this in list/tuple/set syntax:

>>> [1, *(2, 3), 4]
[1, 2, 3, 4]

**d means "treat the key-value pairs in the dictionary as additional named arguments to this function call."

def foo(x, y):
    print(x, y)

>>> d = {'x':1, 'y':2}
>>> foo(**d)
1 2

Since v3.5, you can also do this in dictionary syntax:

>>> d = {'a': 1}
>>> {'b': 2, **d}
{'b': 2, 'a': 1}

In a function signature

*t means "take all additional positional arguments to this function and pack them into this parameter as a tuple."

def foo(*t):
    print(t)

>>> foo(1, 2)
(1, 2)

**d means "take all additional named arguments to this function and insert them into this parameter as dictionary entries."

def foo(**d):
    print(d)

>>> foo(x=1, y=2)
{'x': 1, 'y': 2}

In assignments and for loops

*x means "consume additional elements in the right hand side", but it doesn't have to be the last item. Note that x will always be a list, even if a tuple/set is used for assignment:

>>> w, *x = (1, 2, 3, 4)
>>> w
1
>>> x
[2, 3, 4]

>>> *x, y = (1, 2, 3, 4)
>>> x
[1, 2, 3]
>>> y
4

>>> w, *x, y = (1, 2, 3, 4)
>>> w
1
>>> x
[2, 3]
>>> y
4

>>> for (w, *x, y) in [ (1, 2, 3, 4) ]: print(w, x, y)
... 
1 [2, 3] 4

More on function signatures

Note that parameters that appear after the starred parameter (the one marked *) are keyword-only:

def f(a, *, b): ...

f(1, b=2)  # fine
f(1, 2)    # error: b is keyword-only
# TypeError: f() takes 1 positional argument but 2 were given

Python3.8 added positional-only parameters, meaning parameters that cannot be used as keyword arguments. They appear before a / (a pun on * preceding keyword-only args).

def f(a, /, p, *, k): ...

f(  1,   2, k=3)  # fine
f(  1, p=2, k=3)  # fine
f(a=1, p=2, k=3)  # error: a is positional-only
# TypeError: f() got some positional-only arguments passed as keyword arguments: 'a'
Sign up to request clarification or add additional context in comments.

4 Comments

Nice answer. I'll just add keyword operator to make this answer easier to find when somebody searches for python operator ** expecting * or ** this is called operator in this context.
Great answer. Just to add that in the case of "In a function signature" the common idiom is to use *args for positional arguments and **kwargs for keyword arguments.
@GuzmanOjero this is true, but only if there is no meaningful alternative.
@Mikko They're not actually operators. E.g. you'll notice they're missing from the operator precedence list. I'm not sure if there's a specific term for this syntax, but when used on objects, the construct is called an "unpacking". I believe the difference is that operators can occur in any expression, while these can only occur in certain expressions. And for parameters, the syntax is called something like "star-args" and "star-kwargs" respectively.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.