Converting Python dict to kwargs?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Python, the double-asterisk ** operator lets you unpack a dictionary into keyword arguments when calling a function. This means you can build up a dictionary of parameters and then pass them all at once, which is especially useful when working with configuration objects, API wrappers, or any situation where the set of arguments is determined at runtime. Understanding this mechanism is fundamental to writing flexible, maintainable Python code.
Basic **dict Syntax
The ** operator takes a dictionary and expands its key-value pairs into keyword arguments. Each key becomes a parameter name, and each value becomes the corresponding argument.
This is exactly equivalent to calling greet(name="Alice", greeting="Hello"). The dictionary keys must be strings and must match the function's parameter names.
How It Works Under the Hood
When Python encounters **dict in a function call, it iterates over the dictionary and passes each key-value pair as a keyword argument. If a key does not match any parameter in the function signature, Python raises a TypeError. If a key duplicates a positional argument you already provided, Python also raises a TypeError.
Combining with Positional Arguments
You can mix positional arguments with **dict unpacking. Positional arguments are matched first, then the dictionary fills in the remaining keyword arguments.
You can also combine *args unpacking with **kwargs unpacking in the same call:
**kwargs in Function Definitions
On the receiving side, **kwargs in a function definition collects any extra keyword arguments into a dictionary. This is the mirror image of unpacking.
You can forward these collected kwargs to another function:
Merging Dictionaries with **
Since Python 3.5, you can use ** inside dictionary literals to merge dictionaries. This creates a new dictionary combining all key-value pairs:
Later dictionaries overwrite earlier ones for duplicate keys. In Python 3.9 and later, you can also use the | operator:
TypedDict and Type Hints
When using **dict unpacking with type checkers like mypy, you can use TypedDict to define the expected shape of the dictionary:
With TypedDict and the Unpack type (Python 3.12+ or typing_extensions), you can also type-hint **kwargs:
Real-World Examples
Configuration Dictionaries
API Call Wrappers
Factory Functions
Common Pitfalls
- Using non-string keys in the dictionary, which raises a
TypeErrorsince keyword arguments must be strings. - Passing a key in the dictionary that duplicates a positional argument, causing "got multiple values for argument" errors.
- Mutating the original dictionary after unpacking, which has no effect since unpacking copies the values at call time.
- Assuming dictionary order matters for the function call; it does not, since keyword arguments are matched by name, not position.
- Forgetting that
**kwargscollects extras into a plaindict, losing anyTypedDicttype information at runtime.
Summary
- Use
**dictin a function call to unpack a dictionary into keyword arguments. - Combine with positional arguments and
*listunpacking for full flexibility. - Use
**kwargsin function definitions to accept arbitrary keyword arguments as a dictionary. - Merge dictionaries with
{**a, **b}(Python 3.5+) ora | b(Python 3.9+). - Use
TypedDictwith type checkers to maintain type safety when unpacking dictionaries into function calls.

