Search code examples
pythonpython-3.xprintingreturn

How to get multiple values from "return" as in "print"?


When I run the code below, I get back only the first result. If I replace return (a,d) with print (a,d), I get the full set of results. (I understand that running print (a,d) doesn't save the output anywhere.)

What do I need to change to get the full output and not just the first result?

nums = [(str(i)) for i in range(100,106)]

def foo(aa):
    for a in nums:
        for b in a :
            c= sum(int(b)**2 for b in a)
            d=''.join(sorted(a,reverse=True))
            if (c>5):
                return(a,d) 


output = foo(nums)
print(output)

UPDATE -- I'm expecting the following output:

103 310
103 310
103 310
104 410
104 410
104 410
105 510
105 510
105 510

The return(a,d) gives me just:

103 310

Solution

  • Here you are

    nums = [(str(i)) for i in range(100,106)]
    
    def foo(aa):
        for a in nums:
            for b in a :
                c= sum(int(b)**2 for b in a)
                d=''.join(sorted(a,reverse=True))
                if (c>5):
                    return(a,d)
    
    
    output , output2 = foo(nums)
    print(output, output2)
    

    EDIT

    create a list and insert tuples

    nums = [(str(i)) for i in range(100,106)]
    
    def foo(aa):
        list_of_numbs = list() # create a list
        for a in nums:
            for b in a :
                c= sum(int(b)**2 for b in a)
                d=''.join(sorted(a,reverse=True))
                if (c>5):
                    list_of_numbs.append((a,d)) #insert your desire tuplet in the list
                    #print(a,d)
        return list_of_numbs # return the list
    
    x = foo(nums)
    print(x) # print the list
    # OR
    for i,j in x:     
        print(i,j)