Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass multiple dictionaries to a function as an argument

I have two dictionaries:

SP = dict{'key1': 'value1','key2': 'value2'}
CP = dict{'key1': 'value1','key2': 'value2'}

I have a function that would like to take those two dictionaries as inputs, but I don't know how to do that.

I'd tried:

def test(**SP,**CP):
    print SP.keys(),CP.keys()

test(**SP,**CP)

The real function will be more complicated with lots of modification of two dictionaries. But the simple test function doesn't work for me :(

like image 888
scinthx Avatar asked Jun 22 '14 21:06

scinthx


People also ask

Can we pass dictionary as argument in Python?

Passing Dictionary as an argument In Python, everything is an object, so the dictionary can be passed as an argument to a function like other variables are passed.

Can you pass multiple arguments to a function in Python?

Passing multiple arguments to a function in Python: We can pass multiple arguments to a python function by predetermining the formal parameters in the function definition.

How do you pass the dictionary as Kwargs?

Inside the function, the kwargs argument is a dictionary that contains all keyword arguments as its name-value pairs. Precede double stars ( ** ) to a dictionary argument to pass it to **kwargs parameter. Always place the **kwargs parameter at the end of the parameter list, or you'll get an error.

Can we have multiple arguments in function?

If you define a parameter preceded by * like *args , the function can receive any number of arguments. In the function, multiple arguments are received as a tuple. In the example, a tuple is passed to the sum() function to calculate the sum. It can be used with positional arguments.


4 Answers

Why it doesn't work:

In Python the "**" prefix in the arguments is to make one argument capture several other ones. For example:

SP = {'key1': 'value1','key2': 'value2'}

def test(**kwargs):              #you don't have to name it "kwargs" but that is
    print(kwargs)                #the usual denomination


test(**SP)
test(key1='value1', key2='value2')

The two call on the test function are exactly the same

Defining a function with two ** arguments would be ambiguous, so your interpreter won't accept it, but you don't need it.

How to make it work:

It is not necessary to use ** when working with normal dictionary arguments: in that case you can simply do:

SP = {'key1': 'value1','key2': 'value2'}
CP = {'key1': 'value1','key2': 'value2'}

def test(SP,CP):
    print(SP)
    print(CP)

test(SP,CP)

Dictionaries are objects that you can directly use in your arguments. The only point where you need to be careful is not to modify directly the dictionary in argument if you want to keep the original dictionnary.

like image 90
Anne Aunyme Avatar answered Oct 17 '22 21:10

Anne Aunyme


If you just want to take two dictionaries as separate arguments, just do

def test(SP, CP):
    print SP.keys(),CP.keys()

test(SP, CP)

You only use ** when you want to pass the individual key/value pairs of a dictionary as separate keyword arguments. If you're passing the whole dictionary as a single argument, you just pass it, the same way you would any other argument value.

like image 20
BrenBarn Avatar answered Oct 17 '22 20:10

BrenBarn


You can combine both dictionaries (SP and CP) in a single one using the dict constructor.

test(**dict(SP,**CP))
like image 40
vjerez Avatar answered Oct 17 '22 20:10

vjerez


When you want to pass two different dictionaries to a function that both contains arguments for your function you should first merge the two dictionaries.

For example:

dicA = {'spam':3, 'egg':4}
dicB = {'bacon':5, 'tomato':6}

def test(spam,tomato,**kwargs):
    print spam,tomato

#you cannot use:
#test(**dicA, **dicB)

So you have to merge the two dictionaries. There are several options for this: see How to merge two Python dictionaries in a single expression?

dicC = dicA.copy()
dicC.update(dicB)

#so now one can use:
test(**dicC)

>> 3 6
like image 43
JLT Avatar answered Oct 17 '22 20:10

JLT