Skip to main content

How to Pass a Dictionary as Keyword Arguments in Python with ** (Double Star)

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

When a function's arguments are already in a dictionary, such as settings read from a file, ** passes them all at once: each key becomes a keyword argument with its value. This guide spreads a dictionary into a function call on Python 3.15.2.

Syntax​

Syntax
<function>(**<dictionary>)
PartWhat it means
**Unpacks the dictionary into keyword arguments
<dictionary>Keys that match the function's parameter names, with the values to pass

The Python language reference says: "If the syntax **expression appears in the function call, expression must evaluate to a mapping, the contents of which are treated as additional keyword arguments."

The Example​

describe takes a bike's name, colour and number of gears. The bike is stored as a dictionary with those three keys.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

describe.py
def describe(name, colour, gears):
return f"{name}: {colour}, {gears} gears"


bike = {"name": "City", "colour": "blue", "gears": 7}
print(describe(**bike))

The describe.py script in a macOS-style code window: a describe function with name, colour and gears, a bike dictionary with the same three keys, and print(describe(**bike)).

Step 2: Run It​

Terminal
uv run python describe.py
Output
City: blue, 7 gears

The terminal after uv run python describe.py: City: blue, 7 gears.

describe(**bike) worked as describe(name="City", colour="blue", gears=7): each key matched the parameter of the same name. The command works unchanged in PowerShell.

Mixing ** with Other Arguments​

You can pass some values from a dictionary and the rest by hand. describe(**{"name": "City", "colour": "blue"}, gears=7) printed the same line.

Common Mistakes​

MistakeWhat happensDo this instead
A key the function doesn't take, such as "price"TypeError: describe() got an unexpected keyword argument 'price'Keep only the keys the function names, or add the parameter
A missing key, such as no "gears"TypeError: describe() missing 1 required positional argument: 'gears'Add the key, or give the parameter a default
One star instead of two: describe(*bike)A single * passes the dictionary's keys, not its values: it printed name: colour, gears gearsUse ** for keyword arguments

Troubleshooting​

SymptomLikely causeFix
got an unexpected keyword argument '<key>'The dictionary has a key with no matching parameterRemove the key or rename it
missing 1 required positional argument: '<name>'A parameter has no matching keyAdd the key to the dictionary

FAQ​

Does the order of the keys matter?​

No. Each value goes to the parameter with the same name as its key, wherever the key is in the dictionary.

Can I use it with classes too?​

Yes, anything you call. HistGradientBoostingRegressor(**settings) passes a dictionary of settings to a scikit-learn model the same way.

Conclusion​

describe(**bike) turned the dictionary's three keys into the three keyword arguments describe needs. The keys must match the parameter names exactly, with none extra and none missing.

Additional Resources​