0
0
Pythonprogramming~10 mins

Variable-length keyword arguments (**kwargs) in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Variable-length keyword arguments (**kwargs)
Function called with named arguments
Collect all extra named args into kwargs dict
Inside function: kwargs is a dict
Use kwargs keys and values as needed
Function completes and returns
When a function is called with extra named arguments, Python collects them into a dictionary called kwargs inside the function.
Execution Sample
Python
def greet(**kwargs):
    for key, value in kwargs.items():
        print(f"{key}: {value}")

greet(name='Alice', age=30)
This code collects extra named arguments into kwargs and prints each key and value.
Execution Table
StepActionkwargs contentOutput
1Function greet called with name='Alice', age=30{} (before collection)
2Collect extra named arguments into kwargs{'name': 'Alice', 'age': 30}
3Start loop over kwargs.items(){'name': 'Alice', 'age': 30}
4First iteration: key='name', value='Alice'{'name': 'Alice', 'age': 30}name: Alice
5Second iteration: key='age', value=30{'name': 'Alice', 'age': 30}age: 30
6Loop ends, function ends{'name': 'Alice', 'age': 30}
💡 All items in kwargs processed, function returns None
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5Final
kwargs{}{'name': 'Alice', 'age': 30}{'name': 'Alice', 'age': 30}{'name': 'Alice', 'age': 30}{'name': 'Alice', 'age': 30}
keyN/AN/A'name''age'N/A
valueN/AN/A'Alice'30N/A
Key Moments - 3 Insights
Why is kwargs a dictionary and not a list or other type?
Because **kwargs collects named arguments as key-value pairs, so Python stores them in a dictionary as shown in execution_table step 2.
What happens if no extra named arguments are passed?
kwargs will be an empty dictionary {} and the loop inside the function will not run, so no output is produced (see variable_tracker start state).
Can kwargs keys be accessed like normal dictionary keys?
Yes, inside the function kwargs behaves like a normal dictionary, so you can use methods like items() or access keys directly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of key and value?
Akey='Alice', value='name'
Bkey='age', value=30
Ckey='name', value='Alice'
Dkey='age', value='Alice'
💡 Hint
Check execution_table row with Step 4 under 'Action' and 'Output' columns.
At which step does kwargs get filled with the passed named arguments?
AStep 2
BStep 1
CStep 3
DStep 6
💡 Hint
Look at execution_table row Step 2 where kwargs content changes from empty to filled.
If greet() is called with no arguments, what will kwargs be inside the function?
AA dictionary with keys 'name' and 'age'
BAn empty dictionary {}
CNone
DA list of arguments
💡 Hint
Refer to key_moments answer about no extra named arguments and variable_tracker start state.
Concept Snapshot
def func(**kwargs):
  # kwargs is a dict of extra named args
  for key, value in kwargs.items():
    print(f"{key}: {value}")

Use **kwargs to accept any number of named arguments.
Inside the function, kwargs behaves like a dictionary.
Full Transcript
This example shows how Python collects extra named arguments passed to a function into a dictionary called kwargs. When greet(name='Alice', age=30) is called, kwargs becomes {'name': 'Alice', 'age': 30}. The function loops over this dictionary and prints each key and value. If no extra named arguments are passed, kwargs is empty. This helps functions accept flexible named inputs.