reprinted: note.nkmk.me-Unpack and pass list, tuple, dict to function arguments in Python
In Python, you can unpack list
, tuple
, dict (dictionary)
and pass its elements to function as arguments by adding *
to list or tuple and **
to dictionary when calling function.
This article describes the following contents.
- Unpack list and tuple with *
- With default arguments
- With variable-length arguments
- Unpack dict (dictionary) with **
- With default arguments
- With variable-length arguments
Note that if list
, tuple
, or dict
is specified as an argument without *
or **
, it is not be unpacked and passed to the function as is.
See the following articles for basic usage of functions in Python, default arguments, and variable-length arguments with *
and **
when defining functions.
Unpack list
and tuple
with *
When specifying a list or tuple with *
as an argument, it is unpacked, and each element is passed to each 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
Lists are used in the following sample code, but the same applies to tuples.
If the number of elements does not match the number of arguments, TypeError
raises.
# 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, the default arguments are used if the number of elements is insufficient. If there are many elements, TypeError
raises.
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
If the function has a variable-length argument (*args
), all elements after 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')
Unpack dict (dictionary) with **
By adding **
to dict
(dictionary) and specifying it as an argument, its keys and values are treated as names and values of arguments. Each element is passed as keyword arguments.
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 keys that do not match the argument names, TypeError raises.
# 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 name matching the dictionary key is updated.
If a key does not match the argument name, 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
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'}
标签:function,tuple,Python,arg1,arg2,arg3,two,three,func
From: https://www.cnblogs.com/lfri/p/17213001.html