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.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github