143

My code

1st file:

data = {'school':'DAV', 'standard': '7', 'name': 'abc', 'city': 'delhi'}
my_function(*data)

2nd file:

my_function(*data):
    schoolname  = school
    cityname = city
    standard = standard
    studentname = name

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

I want the my_function() 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

Gabriel
  • 40,504
  • 73
  • 230
  • 404
Patrick
  • 2,464
  • 3
  • 30
  • 47
  • 9
    Don't use variable names that are default objects in Python, such as the word `class`. – Torxed Feb 24 '14 at 11:19
  • 3
    _...and this is my requirement, give answers according to this_ -- Ugh, a bit harsh. But a good question, nonetheless. – pfabri May 22 '20 at 15:46
  • 4
    @pfabri, it's quite clear from reading the question that English is not user's native language, so it's very possible that the harshness was unintended and that's just how they translated it to English. – stason Sep 17 '20 at 06:28

3 Answers3

257

If you want to use them like that, define the function with the variable names as normal:

def my_function(school, standard, city, name):
    schoolName  = school
    cityName = city
    standardName = standard
    studentName = name

Now you can use ** when you call the function:

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

my_function(**data)

and it will work as you want.

P.S. Don't use reserved words such as class.(e.g., use klass instead)

Foad S. Farimani
  • 12,396
  • 15
  • 78
  • 193
RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79
  • i have this line def my_function(*data), modify your code according to this line – Patrick Feb 24 '14 at 12:03
  • 3
    you mean to recompile whole python interpreter to work like you want it to work? – markcial Feb 24 '14 at 12:06
  • 1
    @markcial is there any way to implement in code as i like ? – Patrick Feb 24 '14 at 13:08
  • @Patrick Not a chance, the only way is the one that RemcoGerlih proposed. He has a working sample of code, yours has failed, then is logical to follow their guidelines – markcial Feb 24 '14 at 13:09
  • @RemcoGerlich its giving me error in def my_function(..) line for TypeError: my_function() got an unexpected keyword argument 'school' – Patrick Feb 24 '14 at 14:25
  • Then you didn't put 'school' in the arguments of the function. – RemcoGerlich Feb 24 '14 at 14:30
  • Hello @RemcoGerlich and the rest, is there a way to pass values of only selected keys in a dictionary as parameters to a function? – Mayank Choudhary Feb 28 '17 at 09:53
  • 1
    @MayankChoudhary: either first construct a new dictionary with only the selected keys and pass it with `**`, or just do `my_function(mykey1=mydict["mykey1"], ...)`. There is no automatic way to call a function like that. – RemcoGerlich Mar 01 '17 at 10:42
  • @RemcoGerlich : Thought so. Was wondering if you had something up your sleeve. :-) Thank you for your reply. I went about creating a new dictionaries. – Mayank Choudhary Mar 01 '17 at 11:27
  • Can we dynamically filter using the length of keys in such dictionaries – Scope Aug 28 '22 at 13:29
66

*data interprets arguments as tuples, instead you have to pass **data which interprets the arguments as dictionary.

data = {'school':'DAV', 'class': '7', 'name': 'abc', 'city': 'pune'}


def my_function(**data):
    schoolname  = data['school']
    cityname = data['city']
    standard = data['class']
    studentname = data['name']

You can call the function like this:

my_function(**data)
venpa
  • 4,268
  • 21
  • 23
18

You can just pass it

def my_function(my_data):
    my_data["schoolname"] = "something"
    print my_data

or if you really want to

def my_function(**kwargs):
    kwargs["schoolname"] = "something"
    print kwargs
Jakob Bowyer
  • 33,878
  • 8
  • 76
  • 91
  • made my day :-) – babygame0ver Nov 25 '17 at 11:22
  • 15
    There is a fundamental difference between the two. The first passes a reference the second copies the data. If you pass a reference and the dictionary gets changed inside the function it will be changed outside the function as well which can cause very bad side effects. – Falk Schuetzenmeister Feb 25 '20 at 06:24