What does single(not double) asterisk * means when unpacking dictionary in Python?


Can anyone explain the difference when unpacking the dictionary using single or double asterisk? You can mention their difference when used in function parameters, only if it is relevant here, which I don’t think so.

However, there may be some relevance, because they share the same asterisk syntax.

def foo(a,b)
    return a+b

tmp = {1:2,3:4}
foo(*tmp)        #you get 4
foo(**tmp)       #typeError: keyword should be string. Why it bothers to check the type of keyword? 

Besides, why the key of dictionary is not allowed to be non-string when passed as function arguments in THIS situation? Are there any exceptions? Why they design Python in this way, is it because the compiler can’t deduce the types in here or something?

Asked By: Han XIAO



It is a Extended Iterable Unpacking.

>>> def add(a=0, b=0):
...     return a + b
>>> d = {'a': 2, 'b': 3}
>>> add(**d)#corresponding to add(a=2,b=3)

For single *,

def add(a=0, b=0):
    ...     return a + b
    >>> d = {'a': 2, 'b': 3}
    >>> add(*d)#corresponding to add(a='a',b='b')

Learn more here.

Answered By: Ashutosh Chapagain
def foo(a,b)
   return a+b

tmp = {1:2,3:4}
foo(*tmp)        #you get 4

In this case:
foo(*tmp) mean foo(1, 3)
foo(**tmp) mean foo(1=2, 3=4), which will raise an error since 1 can’t be an argument. Arg must be strings and (thanks @ Alexander Reynolds for pointing this out) must start with underscore or alphabetical character. An argument must be a valid Python identifier. This mean you can’t even do something like this:

def foo(1=2, 3=4):
   <your code>


def foo('1'=2, '3'=4):
   <your code>

See python_basic_syntax for more details.

Answered By: enamoria

When dictionaries are iterated as lists the iteration takes the keys of it, for example

for key in tmp:

is the same as

for key in tmp.keys():

in this case, unpacking as *tmp is equivalent to *tmp.keys(), ignoring the values. If you want to use the values you can use *tmp.values().

Double asterisk is used for when you define a function with keyword parameters such as

def foo(a, b):


def foo(**kwargs):

here you can store the parameters in a dictionary and pass it as **tmp. In the first case keys must be strings with the names of the parameter defined in the function firm. And in the second case you can work with kwargs as a dictionary inside the function.

Answered By: vlizana

I think the ** double asterisk in function parameter and unpacking dictionary means intuitively in this way:

#suppose you have this function
def foo(a,**b):
    for x in b:
#suppose you call this function in the following form
#the m=1 syntax actually means assign parameter by name, like foo(a = whatever, m = 1, n = 2)
#so you can also do foo(whatever,**{"m":1,"n":2})
#the reason for this syntax is you actually do
**b is m=1,n=2 #something like pattern matching mechanism
so b is {"m":1,"n":2}, note "m" and "n" are now in string form
#the function is actually this:
def foo(a,**b):  # b = {"m":1,"n":2}
    for x in b:  #for x in b.keys(), thanks to @vlizana answer

All the syntax make sense now. And it is the same for single asterisk. It is only worth noting that if you use single asterisk to unpack dictionary, you are actually trying to unpack it in a list way, and only key of dictionary are unpacked.

Answered By: Han XIAO


A consequence of this is that although the *expression syntax may appear after explicit keyword arguments, it is processed before the keyword arguments (and any **expression arguments – see below). So:

def f(a, b):
print(a, b)

f(b=1, *(2,))
f(a=1, *(2,))
#Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
#TypeError: f() got multiple values for keyword argument 'a'
f(1, *(2,))
Answered By: Z. K