Patrick Patrick - 5 months ago 21
Python Question

How to pass dictionary items as function arguments in python?

My code

1st file:

data = {'school':'DAV', 'standard': '7', 'name': 'abc', 'city': 'delhi'}

2nd file:

schoolname = school
cityname = city
standard = standard
studentname = name

in the above code, only keys of "data" dictionary were get passed to
, but i want key-value pairs to pass. How to correct this ?

I want the
to get modified like this

my_function(school='DAV', standard='7', name='abc', city='delhi')

and this is my requirement, give answers according to this

EDIT: dictionary key class is changed to standard


If you want to use them like that, define the function with the variable names as normal (but use klass for class, you can't use reserved words):

def my_function(school, city, klass, name):
    schoolname  = school
    cityname = city
    standard = klass
    studentname = name

Now (as long as you rename class to klass in your dictionary) you can use ** when you call the function:


and it will work as you want.