0

In python I am working with a function that takes for instance 3 parameters (actually these functions are being generated on the fly and can have a varying number of parameters) I also have a list containing the arguments I want to use for those parameters. Is there a way to extract the arguments from the list and pass them to the function? I am using a library and so cannot rewrite the functions to use lists instead of individual parameters.

def f(a, b, c):
    # do stuff with a, b, and c

x = [4, 6, 10]

#####
# How do I call f and pass in the elements of x in order?
# i.e. I want to call f(4, 6, 10) without doing f(x[0], x[1], x[2])
#####
j_v_wow_d
  • 511
  • 2
  • 11

1 Answers1

3

You can use * to unpack the values in your list as parameters of your function:

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

x = [4, 6, 10]

f(*x)

Output:

4 6 10

Just be careful that your list has the proper number of parameters.

user3483203
  • 50,081
  • 9
  • 65
  • 94