*args and **kwargs in Python
*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.
*args collects extra positional arguments
Gathered into a tuple, in the order they were passed.
**kwargs collects extra keyword arguments
Gathered into a dictionary, keyed by their names.
Both can sit alongside named parameters
With *args and **kwargs coming last in the definition.
They behave like ordinary collections inside the function
args is a normal tuple; kwargs is a normal dictionary.
The same syntax unpacks in reverse
*some_list and **some_dict spread an existing collection out into separate arguments when calling a function.
def summarize(*args, **kwargs): print("Positional:", args) print("Keyword:", kwargs) summarize(1, 2, 3, name="Ada", role="admin")Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Python yourself — building beats watching.