K. Soerensen
K. Soerensen

Reputation: 203

Function returning two values, store one value in variable and add the other to a variable

I have a function filters that returns two values. I want to store one in a variable data and the other is a string I want to append to the variable filter.

I know the following not possible, but it is basically what I want:

IN: 
filter = "Hello "
data=, filter+= filters(arg, arg, arg)

OUT: 
data = *an array that filters return)
filter = "Hello World"

Upvotes: 1

Views: 559

Answers (4)

nerd100
nerd100

Reputation: 97

def myfunc():
    return "123","World"

var1 = ""
var2 = "Hello "
var1 = myfunc()[0]
var2 += myfunc()[1]

print var1, var2

Output:123 Hello World

Upvotes: 0

boonwj
boonwj

Reputation: 356

You can just assign the 2 values to their respective variables first before doing operations on them.

def return_two():
    return [1,2,3], 'World'

filter = "Hello "
data, sub_string = return_two()

print(data) # [1,2,3]
print(filter + sub_string) # Hello World

Upvotes: 0

Vasilis G.
Vasilis G.

Reputation: 7846

You can use a temporary value to get the second value (string) and then append it to the filter variable.

Note: You should not use filter as variable name because it's one of Python's reserved keywords.

myFilter = "Hello "
data, val = filters(arg, arg, arg)
myFilter += val

Upvotes: 2

amitchone
amitchone

Reputation: 1638

So, here I have a function that returns two variables:

def myFunc():
    return 1, 2

All I have to do to access these variables is this:

var1, var2 = myFunc()

Now, var1 = 1 and var2 = 2

Upvotes: 0

Related Questions