[SOLVED] Passing a dictionary to a function as keyword parameters


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

Here is some code:

d = dict(param='test')

def f(param):


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)

Is this possible?


Figured it out for myself in the end. It is simple, I was just missing the ** operator to unpack the dictionary

So my example becomes:

d = dict(p1=1, p2=2)
def f2(p1,p2):
    print p1, p2

Answered By – Dave Hillier

Answer Checked By – Jay B. (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *