How to extract parameters from a list and pass them to a function call [duplicate]

眉间皱痕 提交于 2019-11-27 03:55:50

You can unpack a tuple or a list into positional arguments using a star.

def add(a, b, c):
    print(a, b, c)

x = (1, 2, 3)
add(*x)

Similarly, you can use double star to unpack a dict into keyword arguments.

x = { 'a': 3, 'b': 1, 'c': 2 }
add(**x) 

I think you mean the * unpacking operator:

>>> l = [1,2,3,4,5]
>>> def add(a,b,c,d,e):
...    print(a,b,c,d,e)
...
>>> add(*l)
1 2 3 4 5
arunkumar

Use the * operator. So add(*x) would do what you want.

See this other SO question for more information.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!