Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8333533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:04:03+00:00 2026-06-09T03:04:03+00:00

I want my function to return multiple dictionaries based on calling arguments. e.g. if

  • 0

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'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:04:04+00:00Added an answer on June 9, 2026 at 3:04 am

    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}]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to return multiple values from a function written in groovy and receive
I'd like to capitalize a string like: ¿hello? I want my function to return
I want to do something like this: $('.dynamicHtmlForm').validate = function() { return true; }
I have a function that return [[]] , and I want to test the
Good Day, I'm creating a VBScript function to return an array. But I want
I want to write a function which would return me a Vector of the
I want to do something like this: class Cls { function fun($php) { return
i want to return wchar_t frm a function. How can i implement it wchar_t
I want to return a set of values from function till the point they
i want to use focus function in jquery to return to same area of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.