Bananach Bananach - 1 year ago 61
Python Question

How to pass large number of keyword arguments automatically in python

I am calling a function


is a large number that may vary from call to call. All arguments except x are keyword arguments.
I didn't write
myself, so I cannot just pass
as a list instead. Is there an automated way to split a list into keyword arguments?

cco cco
Answer Source

First, build a list of name, value pairs, then convert that to a dictionary and pass it to the function.

y_vals = [ 1, 2, 3, 'a', 'b', 'c' ]
arg_dict = dict(('y%d' % i, v) for i, v in enumerate(y_vals))
foo(x, **arg_dict)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download