Learn Python
>>> functions.args_kwargs

*args and **kwargs in Python

#concept

*args and **kwargs let a function accept an arbitrary number of arguments it wasn't explicitly designed with named parameters for. *args collects any extra positional arguments into a tuple; **kwargs collects any extra keyword arguments into a dictionary. The names args and kwargs are just convention — the asterisks are what actually matter.

#how it works
1

*args collects extra positional arguments

Gathered into a tuple, in the order they were passed.

2

**kwargs collects extra keyword arguments

Gathered into a dictionary, keyed by their names.

3

Both can sit alongside named parameters

With *args and **kwargs coming last in the definition.

4

They behave like ordinary collections inside the function

args is a normal tuple; kwargs is a normal dictionary.

5

The same syntax unpacks in reverse

*some_list and **some_dict spread an existing collection out into separate arguments when calling a function.

#example
def summarize(*args, **kwargs):
print("Positional:", args)
print("Keyword:", kwargs)
 
summarize(1, 2, 3, name="Ada", role="admin")
Output
Positional: (1, 2, 3)
Keyword: {'name': 'Ada', 'role': 'admin'}
1summarize(*args, **kwargs) is defined to accept absolutely anything, without naming parameters in advance.
2Calling it with 1, 2, 3 as plain values collects them into the tuple (1, 2, 3), bound to args.
3Calling it with name="Ada", role="admin" collects those into the dictionary {'name': 'Ada', 'role': 'admin'}, bound to kwargs.
#use cases
Writing wrapper or decorator functions that need to pass any arguments through to another function unchanged
Building flexible APIs where the exact set of options might grow over time
Functions like print() itself, which use this same pattern to accept a variable number of values
Forwarding arguments to a parent class's method in an __init__ that doesn't know every possible argument in advance

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Python yourself — building beats watching.