I have a function where I am returning two values. I would like to put the two values directly into two different arrays. I know how to return the output as two different values to be later added to the array, but I don't want to have the temporary place holders. An example is shown below.
def two_outputs():
output_one = 5
output_two = 6
return output_one, output_two
one_array = [] # initialize array
two_array = [] # initialize array
a, b = two_outputs() # get values
one_array.append(a) # store first value in first array
two_array.append(b) # store second value in first array
Ideally I would like to not use a and b and have to append at a later on in the code. I would like to append the output of the function directly to the two arrays. Is this even possible?
Thanks for any help. I hope I did this correctly as this is my first post. You guys have helped me quite a bit with programming issues already.
UPDATE: I guess based on the responses below that it is not possible to do this directly. Thanks for everyone's help in finding other ways to accomplish the goal.
You can only return once from a method. However, the data can still be accessed on separate lines.
We can return more than one values from a function by using the method called “call by address”, or “call by reference”. In the invoker function, we will use two variables to store the results, and the function will take pointer type data. So we have to pass the address of the data.
Python functions can return multiple values. These values can be stored in variables directly. A function is not restricted to return a variable, it can return zero, one, two or more values.
JavaScript functions can return a single value. To return multiple values from a function, you can pack the return values as elements of an array or as properties of an object.
How about using a helper function?
def zippend(lists, values):
assert len(lists) == len(values)
for l,v in zip(lists, values):
l.append(v)
zippend((one_array, two_array), two_outputs())
The function zippend
takes two parameters. The first is an iterable of List
s (what you are referring to as "arrays" are actually List
s in python). The second is an iterable of values to be appended to those lists.
It can take as many lists and values as you want, as long as the number of lists matches the number of values (one value per list).
EDIT: If two_outputs()
were to return a tuple of List
s to be concatenated onto one_array
and two_array
, then you could change the function to use extend
instead of append
:
def zextend(lists, values):
assert len(lists) == len(values)
for l,v in zip(lists, values):
l.extend(v)
Or, if you really wanted to, you could use a single function that had an if-statement that checked what kind of values it was getting and append
ed or extend
ed as appropriate.
Assuming I understand you correctly, you would need to define your arrays prior to the declaration of the function.
one_array, two_array = [], []
def two_outputs():
one_array.append(5)
two_array.append(6)
#call function
two_outputs()
print one_array, two_array
#[5] [6]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With