3

my problem is that I have two function and one of it's return value is two dictionaries in the following way:

def fnct1():
    return dict1, dict2

so it's returning into my other function which return value is the two dictionaries from the previous function and also a new dictionary, so something like this

 def fnct2():
     return dict3, fnct(1)

the problem with this is that is has the following result:

({dict3},({dict1},{dict2})

but I want it to look the following way:

({dict3},{dict1},{dict2})
2
  • 1
    could you show the code you've come up with? also, the help centre has some info on the correct formatting for code. Commented Jun 20, 2017 at 13:34
  • 1
    Sorry for the bad formatting I will be more careful in the future Commented Jun 20, 2017 at 13:54

3 Answers 3

5

Since your function returns a tuple, you need to either return the individual tuple items, or unpack them:

def fnct1():
    dict1 = { "name": "d1" }
    dict2 = { "name": "d2" }
    return dict1, dict2

def fnct2():
    dict3 = { "name": "d3" }
    res = fnct1() 
    return dict3, res[0], res[1] # return the individual tuple elements

# alternative implementation of fnct2:
def fnct2():
    dict3 = { "name": "d3" }
    d1, d2 = fnct1() # unpack your tuple
    return dict3, d1, d2

print(fnct2())
# Output: ({'name': 'd3'}, {'name': 'd1'}, {'name': 'd2'})
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer that was what I was looking for, or is there any way to add dict3 into the tuple instead of unpacking the two what are already there?
@Klftau Tuples are immutable so you cannot change them, however you can add tuples together (in a new tuple) to imitate this behavior. Try return (dict3,) + fnct1().
5

You could unpack the values from fnct1() before returning them:

def fnct2():
    dict1, dict2 = fnct1()
    return dict3, dict1, dict2

Comments

2

If you say the returned value of the 1st function is a. In the 2nd just return a[0], a[1], otherDict.

Comments

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.