Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass dictionary items as function arguments in python? [duplicate]

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

like image 713
Patrick Avatar asked Feb 24 '14 11:02

Patrick


People also ask

How do you pass a dictionary item as a function argument in python?

Passing Dictionary as kwargs “ kwargs ” stands for keyword arguments. It is used for passing advanced data objects like dictionaries to a function because in such functions one doesn't have a clue about the number of arguments, hence data passed is be dealt properly by adding “**” to the passing type.

Does dictionary allow duplicate values in Python?

The straight answer is NO. You can not have duplicate keys in a dictionary in Python.

Does dictionary allow duplicate values?

The Key value of a Dictionary is unique and doesn't let you add a duplicate key entry. To accomplish the need of duplicates keys, i used a List of type KeyValuePair<> .

Does Python pass dictionary by reference?

In essence, one can say that mutable objects like dictionaries, sets, and lists are passed by reference. Immutable objects like int , str , tuple are passed by value.


3 Answers

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)

like image 156
RemcoGerlich Avatar answered Sep 22 '22 00:09

RemcoGerlich


*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)
like image 32
venpa Avatar answered Sep 23 '22 00:09

venpa


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
like image 40
Jakob Bowyer Avatar answered Sep 21 '22 00:09

Jakob Bowyer