Python - returning multiple values ​​from a function to different arrays

I have a function where I return two values. I would like to put two values ​​directly in two different arrays. I know how to return the result as two different values ​​that will later be added to the array, but I do not want to have temporary place owners. 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 not want to use a and b and should add code in the future. I would like to add function output directly to two arrays. Is it possible?

Thanks for any help. Hope I did it right, as this is my first post. You guys helped me a bit in programming.

UPDATE: I think based on the answers below, this cannot be done directly. Thank you all for your help in finding other ways to achieve the goal.

+6
source share
4 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 zippend function accepts two parameters. The first is the iterability of the List (what you call "arrays" is actually List in python). The second is the iterability of the values ​​added to these lists.

It can take as many lists and values ​​as possible if the number of lists corresponds to the number of values ​​(one value per list).

EDIT: if two_outputs() was supposed to return a List tuple that will be concatenated to one_array and two_array , then you can 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 it, you could use one function that had an if statement that checked what values ​​it received and append ed or extend ed, respectively.

+5
source

Assuming I understand you correctly, you will need to define your arrays before declaring 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] 
+1
source

You can always change your function to return a tuple from lists:

 def test(): # some code return [a], [b] a, b = test() 

which will do lists a and b when they are returned

+1
source

It can be executed on one line with the next generator. The call to "anyone" is just that the generator is consumed, and therefore the expressions in it are executed.

 any(lst.append(item) for lst,item in zip((one_array, two_array), two_outputs())) 

NB. I do not recommend this programming style - reading becomes more difficult. Probably, if there is an idiom too frequent, I would write a short helper function for assignment:

 def multi_append(lists, multi_function, *args, **kw): for lst, result in zip(lists, multi_function(*args, **kw)): lst.append(result) 

And on the "body" of the code, simply write:

 multi_append((array_one, array_two), two_outputs) 

For completeness, I am adding a sentence that allows you to use the assignment operator.

In this case, you must create your own List object that has a property that performs the addition. Creating such a class is 2 liners, but then it lists in your code should be from this class:

 class MList(list): last = property(lambda s:s[-1], list.append) array_one, array_two = MList(), MList() array_one.last, array_two.last = two_outputs() 
0
source

Source: https://habr.com/ru/post/903708/


All Articles