This tutorial explains the most important Python argument and unpacking patterns: *, **, *args, **kwargs, keyword-only, positional-only, and common idioms.
There are two types of function arguments, positional vs keyword. See the following as example.
def f(a, b):
print(a, b)
f(1, 2) # positional
f(a=1, b=2) # keyword
f(1, b=2) # mixed
In def f(a, b), parameters are positional-or-keyword by default.
def f(a, b):
print(a, b)
lst = [1, 2]
f(*lst)
Equivalent to:
f(1, 2)
Rule:
*sequence → positional arguments
def f(a, b):
print(a, b)
d = {"a": 1, "b": 2}
f(**d)
Equivalent to:
f(a=1, b=2)
Rule:
**dict → keyword arguments
keys must match parameter names
def f(*args):
print(args)
f(1, 2, 3)
Output:
(1, 2, 3)
Use cases:
unknown number of inputs
wrappers / decorators
forwarding arguments
def f(**kwargs):
print(kwargs)
f(a=1, b=2)
Output:
{'a': 1, 'b': 2}
Use cases:
configuration objects
flexible APIs
passing JSON-like data
def wrapper(*args, **kwargs):
return real_function(*args, **kwargs)
This pattern is used in:
decorators
FastAPI routing
Pydantic models
LLM tool orchestration
def f(a, *, b):
print(a, b)
f(1, b=2) # OK
f(1, 2) # TypeError
Use when:
you want clarity
argument order should not matter
building public APIs
def f(a, b, /):
print(a, b)
f(1, 2) # OK
f(a=1, b=2) # TypeError
Used mainly in:
built-ins
performance-critical code
low-level APIs
a, *middle, b = [1, 2, 3, 4, 5]
print(a, middle, b)
Output:
1 [2, 3, 4] 5
Useful for:
flexible slicing
sequence parsing
*[1, 2] # OK → 1, 2
**[1, 2] # ❌ TypeError (list is not a mapping)
*{"a": 1} # 'a'
**{"a": 1} # a=1
Incoming JSON:
data = {"text": "hello", "threshold": "0.7"}
Pydantic model:
Params(**data)
Why?
models expect named fields
**data converts dict → keyword arguments
validation happens automatically
* → sequence → positional arguments
** → dict → keyword arguments
Or:
Syntax Meaning
*args
collect positional args
**kwargs
collect keyword args
*lst
unpack list
**dict
unpack dict
* in def
keyword-only
/ in def
positional-only
Python’s * and ** are the glue that connects lists, dictionaries, JSON, APIs, and function calls.