0 votes
1 view
in Python by (26.6k points)

I'd like to call a function in python using a dictionary.

Here is some code:

d = dict(param='test')

def f(param):

    print(param)

f(d)

This prints {'param': 'test'} but I'd like it to just print test.

I'd like it to work similarly for more parameters:

d = dict(p1=1, p2=2)

def f2(p1, p2):

    print(p1, p2)

f2(d)

Is it possible?

1 Answer

0 votes
by (16.8k points)

You are just missing this operator ** for unpacking your dictionary:

So, here it becomes like this:

d = dict(p1=1, p2=2)

def f2(p1,p2):

    print p1, p2

f2(**d)

Related questions

0 votes
1 answer
asked Sep 25, 2019 in Python by Sammy (47.8k points)
0 votes
1 answer
0 votes
2 answers
0 votes
1 answer
asked Oct 8, 2019 in Python by Sammy (47.8k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...