*args and **kwargs in Python
In this lesson: Pass variable arguments in Python with *args and **kwargs — syntax, unpacking and real examples with output. In Hindi & English.
Why args and kwargs
Sometimes you do not know in advance how many arguments a function will get. *args and **kwargs let a function accept any number of arguments. *args gathers extra positional values, and **kwargs gathers extra named values.
*args → a tuple of extra positional values. **kwargs → a dict of extra named values.*args — many positional values
def add(*args):
total = 0
for n in args:
total += n
return total
print(add(1, 2, 3)) # 6
print(add(5, 10, 15, 20)) # 50
50
The *args collects however many numbers you pass into a tuple, so the same function works for 3 numbers or 4 — or any count.
**kwargs — many keyword values
def info(**kwargs):
for key, value in kwargs.items():
print(key, "=", value)
info(name="Aman", age=18, city="Delhi")
age = 18
city = Delhi
**kwargs collects named arguments into a dictionary. You can loop its .items() to read each key and value.
Using both together
def demo(a, *args, **kwargs):
print("a:", a)
print("args:", args)
print("kwargs:", kwargs)
demo(1, 2, 3, x=10, y=20)
args: (2, 3)
kwargs: {'x': 10, 'y': 20}
The regular parameter a takes the first value, *args catches the rest of the positional ones, and **kwargs catches the named ones. Order matters: normal, then *args, then **kwargs.
Unpacking with * and **
The same symbols can spread a list or dict back into arguments when calling.
def add(a, b, c):
return a + b + c
nums = [1, 2, 3]
print(add(*nums)) # spreads list -> add(1,2,3)
data = {"a": 1, "b": 2, "c": 3}
print(add(**data)) # spreads dict -> add(a=1,b=2,c=3)
6
Here * and ** do the reverse of collecting — they unpack a list into positional arguments and a dict into keyword arguments.
Common mistakes
- Wrong parameter order. It must be normal params, then
*args, then**kwargs. - Thinking the names are required. Only
*and**matter; the names are convention. - Mixing up the types.
argsis a tuple,kwargsis a dict — access them accordingly. - Forgetting to unpack. Passing a whole list where separate values are needed — use
*list.
Summary
*argslets a function take any number of positional values (as a tuple).**kwargslets it take any number of named values (as a dict).- Order: normal parameters, then
*args, then**kwargs. - Only
*and**matter — the names are convention. - Use
*and**when calling to unpack a list or dict.
Next, review the basics in Python functions, or explore list comprehension.
Frequently Asked Questions
What are *args and **kwargs in Python?
*args collects extra positional arguments into a tuple, and **kwargs collects extra keyword arguments into a dictionary. They make functions flexible.What is the difference between *args and **kwargs?
*args is for positional values passed without names (like add(1, 2, 3)). **kwargs is for named values (like info(name="Aman", age=18)). One gives a tuple, the other a dictionary.Do the names have to be args and kwargs?
* and ** matter. *args and **kwargs are just the common convention; *numbers or **options work exactly the same.What order do parameters go in?
*args, then keyword-only parameters, then **kwargs. Putting them in the wrong order causes a syntax error.What does unpacking with * and ** do?
* spreads a list/tuple into positional arguments and ** spreads a dictionary into keyword arguments. It is the reverse of collecting them.