Marius Folden Simonsen
Marius Folden Simonsen

Reputation: 197

Using string as variable in iPython interactive

I would like to run the following in iPython:

mylist = ['a','b']
def f(a,b):
    do_something

sliderinterval=(0,10,1)
w = interactive(f, a = sliderinterval, b = sliderinterval)

but instead of writing a and b, I would like to take them from mylist. Is that possible?

Upvotes: 1

Views: 39

Answers (1)

Matt
Matt

Reputation: 27843

Make a dict comprehension, and then pass the dictionary to the function by unpacking (**) in as keywords arguments.

mylist = ['a','b']
def f(a,b):
    print(a,b)

sliderinterval=(0,10,1)
d = {k:sliderinterval for k in mylist}
w = interactive(f, **d)

**d is equivalent to writing manually key1=value1, key2=value2... you will often see it in function signature as **kwargs or **kw, for unpacking list you will need only one star and see to as *args.

Upvotes: 1

Related Questions