Search code examples
pythonkeyword-argument

Pass dictionaries in function as arguments


Im trying to create a function that take unknown number of arguments (dictionaries) to merge them in one. Here is my sketch:

weight = {"sara": 60, "nick": 79, "sem": 78, "ida": 56, "kasia": 58, "slava": 95}
height = { "a" : 1, "b": 2, "c":3 }
width = {"u": "long", "q": 55, "qw": "erre", 30: "34"}
a = {10:20, 20:"a"}

def merge(**dict):
    new_dict = {}
    for x in dict:
        for a, b in x.items():
            new_dict[a] = b

    return new_dict

print(merge(weight, height, width, a))

And I got error:

TypeError: merge() takes 0 positional arguments but 4 were given

Why?


Solution

  • Change def merge(**dict): to def merge(*dict): and it is working. Avoid naming it dict since it is a keyword in python.