passing one list of values instead of mutiple arguments to a function?

前端 未结 2 1931
生来不讨喜
生来不讨喜 2020-12-17 03:24

Lets say there\'s a function func() which takes two arguments, a and b. Is there some kind of technique in Python to pass a single lis

相关标签:
2条回答
  • 2020-12-17 03:55

    You can use * to unpack the list into arguments:

    myfunc(*mylist)
    
    0 讨论(0)
  • 2020-12-17 04:10
    def myfunc(a, b):
        return a+b
    
    mylist = [1, 2]
    myfunc(*mylist)
    

    Here mylist can be list, tuple, string etc of length 2.

    0 讨论(0)
提交回复
热议问题