What are *args and **kwargs in Python?
Understand *args and **kwargs in Python: how they collect variable positional and keyword arguments, with code examples and common interview questions answered.
Expected Interview Answer
*args and **kwargs let a function accept a variable number of arguments: *args collects extra positional arguments into a tuple, and **kwargs collects extra keyword arguments into a dictionary.
The names args and kwargs are convention only — what matters are the single star (*) and double star (**). Inside the function, args behaves like a tuple you can loop over, and kwargs behaves like a dict of name-value pairs. The same syntax also works at call sites to unpack a sequence into positional arguments or a dictionary into keyword arguments.
- Write functions that accept any number of arguments
- Build flexible wrappers and decorators
- Forward arguments to other functions cleanly
- Avoid rewriting signatures when inputs vary
- Unpack sequences and dicts directly into calls
AI Mentor Explanation
*args is like a fielding side that can take any number of players onto the ground and lines them up in order, while **kwargs is like the team sheet naming each role — captain, wicketkeeper, twelfth man — by label. One collects positions in sequence, the other pairs each name with its specific duty.
Step-by-Step Explanation
Step 1
Add *args to the signature
Prefix a parameter with one star to collect all extra positional arguments into a tuple.
Step 2
Add **kwargs to the signature
Prefix a parameter with two stars to collect all extra keyword arguments into a dict.
Step 3
Respect the order
Regular parameters come first, then *args, then keyword-only params, then **kwargs.
Step 4
Use them inside the function
Loop over args like a tuple and iterate kwargs.items() like a dictionary.
Step 5
Unpack at call sites
Use *list to spread positionals and **dict to spread keyword arguments into a call.
What Interviewer Expects
- Knowing *args gives a tuple and **kwargs gives a dict
- Understanding the star, not the name, is what matters
- Correct ordering of parameters in a signature
- Ability to forward arguments to another function
- Knowing the same syntax unpacks at call sites
Common Mistakes
- Thinking the names must literally be args and kwargs
- Placing *args after **kwargs in the signature
- Confusing *args (tuple) with **kwargs (dict)
- Forgetting keyword arguments must follow positional ones
- Using a single star when a double star is needed for keywords
Best Answer (HR Friendly)
“*args and **kwargs are Python features that let a function accept any number of inputs. *args gathers extra ordered values into a tuple, and **kwargs gathers extra named values into a dictionary, which makes functions flexible.”
Code Example
def describe(*args, **kwargs):
print('positional:', args)
print('keyword:', kwargs)
describe(1, 2, 3, name='Ada', role='engineer')
# positional: (1, 2, 3)
# keyword: {'name': 'Ada', 'role': 'engineer'}
def total(*numbers):
return sum(numbers)
print(total(4, 5, 6)) # 15def greet(greeting, name):
return f'{greeting}, {name}!'
args = ['Hello', 'Sam']
kwargs = {'greeting': 'Hi', 'name': 'Lee'}
print(greet(*args)) # Hello, Sam!
print(greet(**kwargs)) # Hi, Lee!Follow-up Questions
- What is the correct order of parameters when mixing regular, *args and **kwargs?
- How do you forward *args and **kwargs to another function?
- What is the difference between *args in a definition and *iterable in a call?
- Can you have keyword-only arguments after *args?
- What happens if you pass a duplicate keyword through **kwargs?
MCQ Practice
1. Inside a function, what type is args when defined as *args?
*args collects extra positional arguments into a tuple, which is immutable and ordered.
2. What does **kwargs collect?
**kwargs gathers all extra keyword arguments into a dictionary of name-value pairs.
3. Which call correctly unpacks a dict into keyword arguments?
The double star ** unpacks a dictionary into keyword arguments at the call site.
Flash Cards
What does *args collect? — Extra positional arguments, gathered into a tuple.
What does **kwargs collect? — Extra keyword arguments, gathered into a dictionary.
Do the names have to be args and kwargs? — No — only the * and ** matter; the names are convention.
What is the required parameter order? — Regular params, then *args, then keyword-only params, then **kwargs.