Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function with dictionaries as optional arguments - Python

I'm trying to create a function that might receive as input many or a few dictionaries. I'm using the following code:

def merge_many_dics(dic1,dic2,dic3=True,dic4=True,dic5=True,dic6=True,dic7=True,dic8=True,dic9=True,dic10=True):
"""
Merging up to 10 dictionaries with same keys and different values
:return: a dictionary containing the common dates as keys and both values as values
"""
manydics = {}
for k in dic1.viewkeys() & dic2.viewkeys() & dic3.viewkeys() & dic4.viewkeys() & dic5.viewkeys() & dic6.viewkeys()\
        & dic7.viewkeys() & dic8.viewkeys() & dic9.viewkeys() & dic10.viewkeys():
    manydics[k] = (dic1[k], dic2[k],dic3[k],dic4[k],dic5[k],dic6[k],dic7[k],dic8[k],dic9[k],dic10[k])

return manydics

Note that I'm trying to equal the arguments dic3, dic4, dic5 and so on to "True", so when they are not specified and are called in the function nothing happens. However I'm getting the following error:

Traceback (most recent call last):
File "/Users/File.py", line 616, in <module>
main_dic=merge_many_dics(dic1,dic2,dic3,dic4)
File "/Users/File.py", line 132, in merge_many_dics
& dic7.viewkeys() & dic8.viewkeys() & dic9.viewkeys() & dic10.viewkeys():
AttributeError: 'bool' object has no attribute 'viewkeys'

Anyone to enlight my journey available?

like image 632
aabujamra Avatar asked Dec 13 '15 12:12

aabujamra


People also ask

Can a function take a dictionary as an argument?

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.

How do I make function arguments optional in Python?

You can define Python function optional arguments by specifying the name of an argument followed by a default value when you declare a function. You can also use the **kwargs method to accept a variable number of arguments in a function. To learn more about coding in Python, read our How to Learn Python guide.

How optional arguments can be used within a function?

Functions with optional arguments offer more flexibility in how you can use them. You can call the function with or without the argument, and if there is no argument in the function call, then a default value is used.

Can a function with only keyword arguments be called without arguments?

With keyword arguments, as long as you assign a value to the parameter, the positions of the arguments do not matter. However, they do have to come after positional arguments and before default/optional arguments in a function call.


3 Answers

Using arbitrary argument list, the function can be called with an arbitrary number of arguments:

>>> def merge_many_dics(*dicts):
...     common_keys = reduce(lambda a, b: a & b, (d.viewkeys() for d in dicts))
...     return {key: tuple(d[key] for d in dicts) for key in common_keys}
...
>>> merge_many_dics({1:2}, {1:3}, {1:4, 2:5})
{1: (2, 3, 4)}
like image 167
falsetru Avatar answered Oct 22 '22 16:10

falsetru


You should try the args syntax:

def merge_many_dics(*args):
   iterate over your args to join them

then you can call the function with as many arguments as you like.

A function with *args could be as following:

   def print_all(name, *args):
      print "Hello", name, "here are your args"
      for arg in args:
         print arg

   print_all("Claus", "car", "boat", "house")

this will print:

Hello Clause here are your args

car
boat
house
like image 39
Randrian Avatar answered Oct 22 '22 16:10

Randrian


Here is Python 3.x answer based on @falsetru answer and using the operator.and_ function.

>>> from functools import reduce
>>> import operator
>>> def merge_many_dicts(*dicts):
...     common_keys = reduce(operator.and_, (d.keys() for d in dicts))
...     return {key: tuple(d[key] for d in dicts) for key in common_keys}
... 
>>> merge_many_dicts({1:2}, {1:3}, {1:4, 2:5})
{1: (2, 3, 4)}
like image 29
styvane Avatar answered Oct 22 '22 18:10

styvane