MediumExperience level: 3-5 years · 8-15 years1 min read

What are *args and **kwargs, and how do you use them?

Reviewed by Gurusankar M.

Asked inAmazonDeloitteMicrosoftInfosys
#args#kwargs#variadic#unpacking#functions
Report issue

Short Answer

`*args` collects extra positional arguments into a tuple; `**kwargs` collects extra keyword arguments into a dict. They let a function accept any number of arguments. The same `*`/`**` also unpack a list/dict back into arguments when calling — handy for forwarding args to another function.

On this page

Concept Made Simple

Coffee Chat Question

“What are *args and **kwargs, and how do you use them?”

Remember It Faster

Mind Map Answer

*args
extra positional → tuple
**kwargs
extra keyword → dict
Call side
*list / **dict unpack into args
Order
pos, *args, kw-only, **kwargs

Learn by Doing

Hands-on Keyboard

python
def log(level, *args, **kwargs):
    print(level, args, kwargs)

log("INFO", "a", "b", user="guru", id=7)
# INFO ('a', 'b') {'user': 'guru', 'id': 7}

def add(a, b, c): return a + b + c
nums = [1, 2, 3]
print(add(*nums))          # unpack list → 6
Output
INFO ('a', 'b') {'user': 'guru', 'id': 7}
6

Think Beyond the Expected

What If?

How do *args/**kwargs help when writing a wrapper or decorator?

They let the wrapper accept and forward ANY arguments to the wrapped function without knowing its signature: `def wrapper(*args, **kwargs): return fn(*args, **kwargs)`. That's exactly why decorators use them — one wrapper works for functions with any parameters.

Real World

Decorators, logging helpers, and thin API wrappers all rely on *args/**kwargs to pass arguments through transparently; unpacking (*/** at the call site) is the flip side used to spread collected data back into calls.

Interviewer's Expectation

Keywords they're listening for:

  • *args → tuple
  • **kwargs → dict
  • accept variable arity
  • unpack at call site
  • used by decorators/wrappers

Common Mistakes

  • Confusing collect (def) vs unpack (call)
  • Wrong parameter order in the signature
  • Overusing **kwargs and losing a clear API

Best Practices

  • Use for wrappers/decorators and flexible APIs
  • Prefer explicit params when the signature is known
  • Document expected kwargs

Follow-up Questions

  1. What are keyword-only and positional-only parameters?
  2. How does argument order/precedence work?
  3. How does this pair with functools.wraps in decorators?

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: Functions (Python)
Interview question: "What are *args and **kwargs, and how do you use them?"

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.