What does ** and * do for parameters in Python?

Better Stack Team
Updated on January 26, 2023

In Python, the * symbol is used to indicate that an argument can be passed to a function as a tuple. The ``** symbol is used to indicate that an argument can be passed to a function as a dictionary.

Here's an example of how you might use these symbols in a function definition:

 
def my_function(arg1, *args, **kwargs):
    # arg1 is a required argument
    # args is a tuple of optional positional arguments
    # kwargs is a dictionary of optional keyword arguments
    pass

You can then call the function like this:

 
my_function(1, 2, 3, 4, 5, keyword1=6, keyword2=7)

This would pass the following values to the function:

 
arg1 = 1
args = (2, 3, 4, 5)
kwargs = {'keyword1': 6, 'keyword2': 7}

You can use the * and ``** syntax when calling a function as well. For example:

 
def my_function(arg1, arg2):
    print(arg1)
    print(arg2)

my_list = [1, 2]
my_dict = {'arg2': 3}

my_function(*my_list, **my_dict)

This would output the following:

 
1
3

The * and `** symbols are used to unpack the values in the **mylist** and **mydict** variables, respectively, and pass them as individual arguments to the **my_function`** function.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →