The Purpose of * and ** in Python

Najwa Riyaz Oct 10, 2023
  1. the Definition of * in Python
  2. the Definition of ** in Python
  3. the Uses of * and ** in Function Calls in Python
The Purpose of * and ** in Python

This article explains the purpose of * and ** in Python.

In Python, we use the single asterix (*) and the double asterix(**) to signify a variable number of arguments.

We can pass any number of arguments in a Python function in either of the following ways.

  1. Positional arguments(*)
  2. Keyword arguments (**)

the Definition of * in Python

Use the symbol * in Python to allow a variable number of positional arguments/parameters to the function.

Follow the example below.

def function_singleasterix(*someargs):
    for i in someargs:
        print(i)

Now, the driver code, either with list or tuple is as follows.

listdata = ["Alex", "Tom", "John", "Alice"]
function_singleasterix(listdata)

Output:

['Alex', 'Tom', 'John', 'Alice']

Use positional-only arguments if you don’t want the users to know the name of the parameters.

For example, it is advisable to use positional-only variables in APIs - this avoids breakage in case of any changes in the API if the parameter’s name is modified.

the Definition of ** in Python

Use the symbol ** in Python to allow a variable number of keyword arguments/parameters to the function. Note that the argument after must be a mapping (dictionary key-value pair) items, not a tuple or a list.

Follow the example code below.

def function_doubleasterix(**keywordargs):

    print("The keys in the kwargs dicionary are -", keywordargs.keys())
    print("The values in the kwargs dicionary are -", keywordargs.values())

    print("--The key value assignment in the 'keywordargs' dictionary are as follows--")
    for key, value in keywordargs.items():
        print("%s == %s" % (key, value))

In the example above, keywordargs is associated with a dictionary like in the program below.

function_doubleasterix(SNo001="Alex", SNo002="Tom")

Output:

The keys in the 'keywordargs' dicionary are - dict_keys(['SNo001', 'SNo002'])
The values in the 'keywordargs' dicionary are - dict_values(['Alex', 'Tom'])
--The key value assignment in the 'keywordargs' dictionary are as follows--
SNo001 == Alex
SNo002 == Tom

In the example above, the **keywordargs provides keyword arguments as dictionary key-value pairs.

the Uses of * and ** in Function Calls in Python

The symbols * and ** are used in function calls as well. Use them to pass a variable number of arguments to a function using either of the following.

  • a list - *
  • a tuple - *
  • a dictionary - **

Here are several examples you can follow.

Call the Function With a Variable list as Input. Use * as Follows

varlist = ["Tom", "John", "Alice"]
functiondef(*varlist)

Call the Function With a Variable dictionary as Input. Use ** as Follows

vardict = {"a": "Tom", "b": "John", "c": "Alice"}
functiondef(**vardict)

Call the Function With a Variable tuple as Input. Use * as Follows

vartuple = ("Tom", "John", "Alice")
functiondef(*vartuple)

The output for all the above cases is:

SNo1 = Tom
SNo2 = John
SNo3 = Alice

Related Article - Python Argument