Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - returning multiple values from function to different arrays

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.

like image 900
ruffryder Avatar asked Dec 13 '11 19:12

ruffryder


People also ask

Can a function return multiple arrays Python?

You can only return once from a method. However, the data can still be accessed on separate lines.

Can a function return multiple values of different types?

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.

Can 1 function return multiple values in Python?

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.

Can a function return multiple arrays?

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.


2 Answers

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 Lists (what you are referring to as "arrays" are actually Lists 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 Lists 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 appended or extended as appropriate.

like image 122
dhg Avatar answered Sep 22 '22 13:09

dhg


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]
like image 37
Robert Avatar answered Sep 22 '22 13:09

Robert