What is kwargs in python

Last updated: April 1, 2026

Quick Answer: **kwargs in Python allows functions to accept an arbitrary number of keyword arguments passed as a dictionary. The double asterisks indicate that arguments are collected into a kwargs dictionary with parameter names as keys.

Key Facts

Understanding **kwargs in Python

kwargs is a Python feature that allows functions to accept an arbitrary number of keyword arguments. The double asterisks (**) are a special syntax that signals to Python to collect named arguments into a dictionary.

How **kwargs Works

When you define a function with **kwargs, any keyword arguments passed to the function are collected into a dictionary. Inside the function, you access these arguments like any other dictionary. For example:

Basic Example

The name 'kwargs' is a Python convention, though technically you could use any name after the double asterisks. However, using **kwargs is standard practice and makes code more readable. The double asterisks are what matter syntactically; they tell Python to expect keyword arguments.

Combining with Other Parameters

Functions can use regular positional arguments, *args (arbitrary positional arguments), and **kwargs (arbitrary keyword arguments) together. The order matters: regular parameters first, then *args, then **kwargs. Example: def function(a, b, *args, **kwargs). This flexibility makes functions highly adaptable to different calling conventions.

Common Use Cases

kwargs is particularly useful in several scenarios:

Iterating Through kwargs

Since **kwargs creates a dictionary, you can iterate through key-value pairs using .items(). This is useful when you need to process all passed arguments dynamically without knowing their names in advance. This flexibility is one of the most powerful aspects of using **kwargs.

Best Practices

While **kwargs provides flexibility, use it judiciously. Over-relying on **kwargs can make function signatures unclear and code harder to understand. Document what keyword arguments your function expects, and consider using explicit parameters for commonly used arguments.

Related Questions

What is the difference between *args and **kwargs?

*args collects positional arguments into a tuple, while **kwargs collects keyword arguments into a dictionary. *args comes before **kwargs in function definitions.

Can you use kwargs without the double asterisks?

No, the double asterisks (**) are required syntax. They tell Python to collect keyword arguments. The name 'kwargs' is just a convention; you could use **options or **params, but the double asterisks are essential.

How do you pass kwargs to another function?

You can pass **kwargs to another function by using the ** unpacking operator again. For example: other_function(**kwargs) will unpack the dictionary back into keyword arguments.

Sources

  1. Python Documentation - *args and **kwargs PSF
  2. Wikipedia - Python Programming Language CC-BY-SA-4.0