2

I have an array that should be expanded to provide multiple arguments to a function call - like the following:

def x(a,b,c):
    print "%d %d %d" %(a,b,c)

t = [1,2,3]

x(t[:])  # Will not work - this is only one value

Basically I am looking for the python equivalent to the

 :_*

construct in scala

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560

2 Answers2

9

It's called unpacking:

>>> def x(a, b, c):
...     print(a, b, c)
...
>>> l = [1, 2, 3]
>>> x(*l)
(1, 2, 3)
Celeo
  • 5,583
  • 8
  • 39
  • 41
2

In addition to unpacking, you can change your function to accept a list:

def x(nums):
    # cut off list and join three elements
    print ' '.join(str(i) for i in nums[:3])
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70