Mathew
Mathew

Reputation: 1430

Passing an arbitrary number of parameters to a function in python

I have a python function which takes as input an arbitrary number of parameters. I beleive this is implemented via *args (thanks @Anurag Wagh for pointing this out). I would like to be able to pass to it a list of values. Here is the specific code I have:

from sympy import solve_poly_system, symbols
x,y,z = symbols('x y z')
print(solve_poly_system([y**2 - x**3 + 1, y*x], x, y))

Instead of passing x and y to solve_poly_system, I want to be able to pass it a list L something like so:

print(solve_poly_system([y**2 - x**3 + 1, y*x], L))

where L = [x,y].

Is this possible? Please note, although I am interested in the specific case of the solve_poly_system function, I am more broadly interested in the passing a list to any function in python that takes an arbitrary number of inputs.

To put this another way, I want to know how to pass a list of values to a function which takes *args as input. In particular I am interested in the case where the length of the list is not known until run time and may change during run time.

Upvotes: 0

Views: 431

Answers (1)

Aniket Tiratkar
Aniket Tiratkar

Reputation: 858

From your question, it looks like you know how to use *args in function definition and you are looking for a way to pass all the elements of a list to that function regardless of the length of the list. If that's your question, then here's your answer:

def function(x, *args):
    print(x)
    print(args)

x = 10
l = [5, 6, 7, 8]
function(x, *l)

Output:

10
(5, 6, 7, 8)

Upvotes: 1

Related Questions