Skip to main content

*args and **kwargs

Normally a function call has to supply the right number of arguments.

Sometimes, though, you can't tell in advance how many will be passed.

That is the problem *args and **kwargs solve: they let a function accept an unknown number of arguments.


Arbitrary Arguments - *args

When you can't predict how many arguments a function will get, put a * in front of the parameter name.

The function then receives the arguments as a tuple and can reach each item by its position.

Python documentation often abbreviates Arbitrary Arguments to *args.

Example

Accepting any number of arguments with *args:

Prompt
Write a function using *kids to accept any number of arguments. Print the third child. Call it with "Emil", "Tobias", "Linus".
Output: complete Python program

What is *args?

A function with a *args parameter can take as many positional arguments as the caller wants to send.

All of them end up in args, which is a tuple inside the function:

Example

Reading single arguments out of *args:

Prompt
Write a function using *args and print the type, first, second, and all arguments. Call it with "Emil", "Tobias", "Linus".
Output: complete Python program

Using *args with Regular Arguments

Regular parameters and *args can appear side by side.

The regular ones have to be listed first.

Here greeting takes the value "Hello", and names collects all the remaining values.

Example

Prompt
Write a function with a regular parameter greeting and *names. Print the greeting with each name. Call with "Hello", "Emil", "Tobias", "Linus".
Output: complete Python program

Practical Example with *args

Flexible functions are where *args really helps:

Example

Adding up however many values are passed:

Prompt
Write a function using *numbers that calculates the sum of any number of values. Test with (1,2,3), (10,20,30,40), and (5).
Output: complete Python program

Example

Looking for the largest value:

Prompt
Write a function using *numbers that finds the maximum value. Return None for empty input. Test with (3, 7, 2, 9, 1).
Output: complete Python program

Arbitrary Keyword Arguments - **kwargs

When you can't predict how many keyword arguments a function will get, put two asterisks ** in front of the parameter name.

The function then receives them as a dictionary and can look up each item by its key.

Python documentation often abbreviates Arbitrary Keyword Arguments to **kwargs.

Example

Accepting any number of keyword arguments with **kwargs:

Prompt
Write a function using **kid to accept keyword arguments. Print the last name. Call with fname="Tobias", lname="Refsnes".
Output: complete Python program

What is **kwargs?

A function with a **kwargs parameter can take as many keyword arguments as the caller wants to send.

All of them end up in kwargs, which is a dictionary inside the function:

Example

Reading values out of **kwargs:

Prompt
Write a function using **myvar and print the type, name, age, and all data. Call with name="Tobias", age=30, city="Bergen".
Output: complete Python program

Using **kwargs with Regular Arguments

Regular parameters and **kwargs work together as well.

Here too, the regular ones are listed first:

Example

Prompt
Write a function with username and **details. Print the username and all additional details. Call with "emil123" and extra keyword args.
Output: complete Python program

Combining *args and **kwargs

One function can have both *args and **kwargs.

List the parameters in this order:

  1. regular parameters
  2. *args
  3. **kwargs

Example

Prompt
Write a function with title, *args, and **kwargs. Print all three. Call with mixed positional and keyword arguments.
Output: complete Python program

Unpacking Arguments

You can also write * and ** in a function call. There they unpack (expand) a list or dictionary into individual arguments.

Remember: In a definition, * and ** collect arguments. In a call, they unpack them.

Example

Turning a list into separate arguments with *:

Prompt
Write a function that takes three parameters and returns their sum. Use * to unpack a list [1, 2, 3] into the function call.
Output: complete Python program

Example

Turning a dictionary into keyword arguments with **:

Prompt
Write a function that takes fname and lname and prints a greeting. Use ** to unpack a dictionary into the function call.
Output: complete Python program