Python Tutorials · Python Functions

Python *args / **kwargs

Learn all about Python *args / **kwargs in this comprehensive tutorial.

5 min read beginner
  • By default, a function must be called with the correct number of arguments.
  • If you do not know how many arguments will be passed into your function, add a * before the parameter name.
  • The *args parameter allows a function to accept any number of positional arguments.
  • You can combine regular parameters with *args.
  • *args is useful when you want to create flexible functions:
  • If you do not know how many keyword arguments will be passed into your function, add two asterisks ** before the parameter name.
  • The **kwargs parameter allows a function to accept any number of keyword arguments.
  • You can combine regular parameters with **kwargs.
  • You can use both *args and **kwargs in the same function.
  • The * and ** operators can also be used when calling functions to unpack (expand) a list or dictionary into separate arguments.

*args and **kwargs

By default, a function must be called with the correct number of arguments.

However, sometimes you may not know how many arguments that will be passed into your function.

*args and **kwargs allow functions to accept a unknown number of arguments.

Arbitrary Arguments - *args

If you do not know how many arguments will be passed into your function, add a * before the parameter name.

This way, the function will receive a tuple of arguments and can access the items accordingly:

python
Note: Arbitrary Arguments are often shortened to *args in Python documentation.

What is *args?

The *args parameter allows a function to accept any number of positional arguments.

Inside the function, args becomes a tuple containing all the passed arguments:

python

Using *args with Regular Arguments

You can combine regular parameters with *args.

Regular parameters must come before *args:

python

In this example, "Hello" is assigned to greeting, and the rest are collected in names.

Practical Example with *args

*args is useful when you want to create flexible functions:

python
python

Arbitrary Keyword Arguments - **kwargs

If you do not know how many keyword arguments will be passed into your function, add two asterisks ** before the parameter name.

This way, the function will receive a dictionary of arguments and can access the items accordingly:

python
Note: Arbitrary Keyword Arguments are often shortened to **kwargs in Python documentation.

What is **kwargs?

The **kwargs parameter allows a function to accept any number of keyword arguments.

Inside the function, kwargs becomes a dictionary containing all the keyword arguments:

python

Using **kwargs with Regular Arguments

You can combine regular parameters with **kwargs.

Regular parameters must come before **kwargs:

python

Combining *args and **kwargs

You can use both *args and **kwargs in the same function.

The order must be: regular parameters *args **kwargs

python

Unpacking Arguments

The * and ** operators can also be used when calling functions to unpack (expand) a list or dictionary into separate arguments.

If you have values stored in a list, you can use * to unpack them into individual arguments:

python

If you have keyword arguments stored in a dictionary, you can use ** to unpack them:

python
Note: Remember: Use * and ** in function definitions to collect arguments, and use them in function calls to unpack arguments.

Module quiz

2 questions
1

Which of the following is true about Python *args / **kwargs?

2

What is the most common pitfall when working with Python *args / **kwargs?

Answer all questions to submit.