Python: Unpack Dictionary (asterisk)
Unpack dict to function arguments (**)
turn dictionary into arguments for function call, as keyword arguments.
each dictionary pair is turned into key value pair (key=value).
def ff(aa, bb): print(aa, bb) xdict = {"aa": 3, "bb": 4} # Equivalent to ff(aa=3, bb=4) ff(**xdict) # 3 4
Merg dictionaries (**)
xx = {"dog": 3, "cat": 4} yy = {"cat": 9, "bird": 5} zz = {**xx, **yy} print(zz) # {'dog': 3, 'cat': 9, 'bird': 5}
Unpack keys (*)
use single asterisk (*) to unpack keys, into list, or variables, or function arguments.
# unpact dictionary keys into list xdict = {"x": 1, "y": 2} keys = [*xdict] print(keys) # ['x', 'y']
# unpact dictionary keys into variables xdict = {"x": 1, "y": 2} a, b = [*xdict] print(a, b) # x y
# unpact dictionary keys into function arguments xdict = {"x": 1, "y": 2} def f(a, b): return [a, b] print(f(*xdict)) # ['x', 'y']