Expand and Pass a List and Dictionary As Arguments in Python
In Python, you can expand a list, tuple, and dictionary (dict
) and pass their elements as arguments by prefixing a list or tuple with an asterisk (*
), and prefixing a dictionary with two asterisks (**
) when calling functions.
For a basic understanding of Python functions, default parameter values, and variable-length arguments using *
and **
during function definition, refer to the following articles.
- Define and call functions in Python (def, return)
- Default parameter values for functions in Python
*args
and**kwargs
in Python (Variable-length arguments)
Expand a list and tuple with *
By specifying a list or tuple prefixed with *
as an argument, 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 sample code uses lists, but the same applies to tuples.
If the number of elements does not match the number of arguments, a TypeError
is raised.
# 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
With default arguments
If the function has default arguments, these default values are used when there are not enough elements. If there are too many elements, a TypeError
is raised.
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
With variable-length arguments (*args
)
If the function has a variable-length argument (*args
), all elements following 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 a dictionary (dict
) with **
By specifying a dictionary (dict
) prefixed with **
as an argument, each key-value pair is passed as a keyword 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 are not enough keys or if the keys do not match the argument names, a TypeError
is raised.
# 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'
With default arguments
If the function has default arguments, only the value of the argument whose name matches a dictionary key is updated. If a key does not match the argument name, a TypeError
is raised.
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'
With variable-length arguments (**kwargs
)
If the function has a variable-length argument (**kwargs
), all elements with keys that do not match the argument name are 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'}