Expanding and passing lists, tuples, and dictionaries as function arguments in Python

Money and Business

In Python, lists (arrays), tuples, and dictionaries can be expanded (unpacked) and their respective elements can be passed together as function arguments.

When calling a function, specify the argument with * for lists and tuples and ** for dictionaries. Note the number of asterisks *.

The following details are described here.

  • Expand (unpack) a list or tuple with * (one asterisk)
    • For functions with default arguments
    • For functions with variable-length arguments
  • Expand (unpack) dictionary with ** (two asterisks)
    • For functions with default arguments
    • For functions with variable-length arguments

See the following article for basic usage of Python functions, default arguments, and variable length arguments with *,** when defining functions.

Expand (unpack) a list or tuple with * (one asterisk)

When a list or tuple is specified as an argument with *, it is expanded and each element is passed as a separate argument.

def func(arg1, arg2, arg3):
    print('arg1 =', arg1)
    print('arg2 =', arg2)
    print('arg3 =', arg3)

l = ['one', 'two', 'three']

func(*l)
# arg1 = one
# arg2 = two
# arg3 = three

func(*['one', 'two', 'three'])
# arg1 = one
# arg2 = two
# arg3 = three

t = ('one', 'two', 'three')

func(*t)
# arg1 = one
# arg2 = two
# arg3 = three

func(*('one', 'two', 'three'))
# arg1 = one
# arg2 = two
# arg3 = three

The following explanation is for a list, but the same applies to a tuple.

If the number of elements does not match the number of arguments, a TypeError error occurs.

# func(*['one', 'two'])
# TypeError: func() missing 1 required positional argument: 'arg3'

# func(*['one', 'two', 'three', 'four'])
# TypeError: func() takes 3 positional arguments but 4 were given

For functions with default arguments

If a default argument is set, the default argument is used if the number of elements is insufficient. If the number of elements is too large, a TypeError error occurs.

def func_default(arg1=1, arg2=2, arg3=3):
    print('arg1 =', arg1)
    print('arg2 =', arg2)
    print('arg3 =', arg3)

func_default(*['one', 'two'])
# arg1 = one
# arg2 = two
# arg3 = 3

func_default(*['one'])
# arg1 = one
# arg2 = 2
# arg3 = 3

# func_default(*['one', 'two', 'three', 'four'])
# TypeError: func_default() takes from 0 to 3 positional arguments but 4 were given

For functions with variable-length arguments

If a variable-length argument is set, all elements after the element for the positional argument are passed to the variable-length argument.

def func_args(arg1, *args):
    print('arg1 =', arg1)
    print('args =', args)

func_args(*['one', 'two'])
# arg1 = one
# args = ('two',)

func_args(*['one', 'two', 'three'])
# arg1 = one
# args = ('two', 'three')

func_args(*['one', 'two', 'three', 'four'])
# arg1 = one
# args = ('two', 'three', 'four')

Expand (unpack) dictionary with ** (two asterisks)

When a dictionary dict is specified as an argument with **, the element keys are expanded as argument names and values as argument values, and each is passed as a separate argument.

def func(arg1, arg2, arg3):
    print('arg1 =', arg1)
    print('arg2 =', arg2)
    print('arg3 =', arg3)

d = {'arg1': 'one', 'arg2': 'two', 'arg3': 'three'}

func(**d)
# arg1 = one
# arg2 = two
# arg3 = three

func(**{'arg1': 'one', 'arg2': 'two', 'arg3': 'three'})
# arg1 = one
# arg2 = two
# arg3 = three

If there is no key that matches the argument name or there is a key that does not match, a TypeError error will result.

# func(**{'arg1': 'one', 'arg2': 'two'})
# TypeError: func() missing 1 required positional argument: 'arg3'

# func(**{'arg1': 'one', 'arg2': 'two', 'arg3': 'three', 'arg4': 'four'})
# TypeError: func() got an unexpected keyword argument 'arg4'

For functions with default arguments

Image in which only the values of argument names that match the keys in the dictionary are updated.

A key that does not match the argument name will result in a TypeError error.

def func_default(arg1=1, arg2=2, arg3=3):
    print('arg1 =', arg1)
    print('arg2 =', arg2)
    print('arg3 =', arg3)

func_default(**{'arg1': 'one'})
# arg1 = one
# arg2 = 2
# arg3 = 3

func_default(**{'arg2': 'two', 'arg3': 'three'})
# arg1 = 1
# arg2 = two
# arg3 = three

# func_default(**{'arg1': 'one', 'arg4': 'four'})
# TypeError: func_default() got an unexpected keyword argument 'arg4'

For functions with variable-length arguments

If variable-length arguments are set, any element with a key other than the argument name specified as the argument is passed to the variable-length argument.

def func_kwargs(arg1, **kwargs):
    print('arg1 =', arg1)
    print('kwargs =', kwargs)

func_kwargs(**{'arg1': 'one', 'arg2': 'two', 'arg3': 'three'})
# arg1 = one
# kwargs = {'arg2': 'two', 'arg3': 'three'}

func_kwargs(**{'arg1': 'one', 'arg2': 'two', 'arg3': 'three', 'arg4': 'four'})
# arg1 = one
# kwargs = {'arg2': 'two', 'arg3': 'three', 'arg4': 'four'}

func_kwargs(**{'arg1': 'one', 'arg3': 'three'})
# arg1 = one
# kwargs = {'arg3': 'three'}