1

I want my function to return multiple dictionaries based on calling arguments. e.g. if i call the function with no argument, it should return all the dictionaries else if i call it with a list of arguments, it should return the corresponding dictionaries from the function. Apparently if i call with no args or one arg, it works fine but i am having problem with using it with multiple values. Following example shows this problem.

def mydef(arg1=None):
    a = {'a1':1, 'a2':2}
    b = {'b1':1, 'b2':2}
    c = {'c1':1, 'c2':2}
    d = {'d1':1, 'd2':2}
    if arg1 is None:
        return a,b,c,d
    else:
        for x in arg1:
            if x == 'a':
                return a
            elif x == 'b':
                return b

 w,x,y,z = mydef()
 print type(w)
 print w

 s,t = mydef(['a', 'b'])
 print type(s)
 print s
 print type(t)
 print t

Doubt: Lists are returned instead of dicts:

def mydef(args=None):
     dicts = { 'a' :{'a1' : 1, 'a2' :2}, 'b' : {'b1' : 1, 'b2' :2}}
     if args is None:
         args = ['a', 'b']
     return [dicts[d] for d in args]


x,y = mydef()
type(x)
>> type 'dict'

type(y)
>> type 'dict'

x = mydef(['a'])
type(x)
>> type 'list'

2 Answers 2

11

A function only gets to return once. You can't loop and try to return something on each iteration of the loop; the first one returns and that ends the function. If you want to "return multiple values", what you really have to do is return one value that contains them all, like a list of the values, or a tuple of the values.

Also, it'd be better to put your dictionaries in a dictionary (sup dawg) instead of using local variables to name them. Then you can just pick them out by key.

Here's a way of doing it that returns a list of the selected dicts:

>>> def returnDicts(args=None):
...     dicts = {
...         'a': {'a1':1, 'a2':2},
...         'b': {'b1':1, 'b2':2},
...         'c': {'c1':1, 'c2':2},
...         'd': {'d1':1, 'd2':2}
...     }
...     if args is None:
...         args = ['a', 'b', 'c', 'd']
...     return [dicts[d] for d in args]
>>> returnDicts()
[{'a1': 1, 'a2': 2},
 {'b1': 1, 'b2': 2},
 {'c1': 1, 'c2': 2},
 {'d1': 1, 'd2': 2}]
>>> returnDicts(['a'])
[{'a1': 1, 'a2': 2}]
>>> returnDicts(['a', 'b'])
[{'a1': 1, 'a2': 2}, {'b1': 1, 'b2': 2}]
Sign up to request clarification or add additional context in comments.

3 Comments

Minor nitpick: I'd write args = dicts.keys() so that your function will work as is if you add more dictionaries
dicts.keys() will work, but note that then you can't know the order of the returned dicts. I guess you could use sorted(dicts.keys()).
When i pass any argument or modify the original list in "if args is None" so as to have only few values, then returned value is list and not dictionary. However when no arguments are passed and original list is not modified, then returned value is dict. Why it is behaving differently if no args is passed or args is modified?
0

why not make them dicts of dicts like so

def mydicts(arg1=None):
    dicter = {'a': {'a1':1, 'a2':2},
              'b': {'b1':1, 'b2':2},
              'c': {'c1':1, 'c2':2},
              'd': {'d1':1, 'd2':2},
              }

    #if arg1 is None return all the dictionaries.
    if arg1 is None:
        arg1 = ['a', 'b', 'c', 'd']

    # Check if arg1 is a list and if not make it one 
    # Example you pass it a str or int

    if not isinstance(arg1, list):
        arg1 = [arg1]

    return [dicter.get(x, {}) for x in arg1]

Note, this will also return a list of items back to you.

5 Comments

I tried your solution, here when i pass argments, then returned value is list and not dictionary. Moreover the returned list also contained only keys, so explicit conversion will also not help
I fixed a syntax error, and if you read it said "Note, this will also return a list of items back to you." I see full dicts when I run this as well and not just keys. In [8]: mydicts() Out[8]: [ {'a1': 1, 'a2': 2}, {'b1': 1, 'b2': 2}, {'c1': 1, 'c2': 2}, {'d1': 1, 'd2': 2} ]
I tried with your edited code, it is still returning lists when arg is passed. Please see my edits in original question showing how i am using it. I used your as it as but just used it as described in my edit.
so as I said it returns a list of dicts, and I just checked it and I see both keys and values in the list of dicts returned. I'm not sure where you are only getting keys. it returns the dicts in order of the lists you pass. This on purpose because a dict isn't ordered, while a list is. In [2]: mydicts('a') Out[2]: [{'a1': 1, 'a2': 2}] In [3]: mydicts(['a', 'g']) Out[3]: [{'a1': 1, 'a2': 2}, {}] In [4]: mydicts(['a', 'b']) Out[4]: [{'a1': 1, 'a2': 2}, {'b1': 1, 'b2': 2}] In [5]: mydicts() Out[5]: [{'a1': 1, 'a2': 2}, {'b1': 1, 'b2': 2}, {'c1': 1, 'c2': 2}, {'d1': 1, 'd2': 2}]
last example: Ordered based on input you provide In [6]: mydicts(['b', 'a']) Out[6]: [{'b1': 1, 'b2': 2}, {'a1': 1, 'a2': 2}]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.