Python - returning multiple values from function to different arrays

14,192

Solution 1

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.

Solution 2

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]

Solution 3

You could always alter your function to return a tuple of lists:

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

a, b = test()

that will make both a and b lists when they're returned

Share:
14,192
ruffryder
Author by

ruffryder

Updated on June 09, 2022

Comments

  • ruffryder
    ruffryder almost 2 years

    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.

  • Laurence Gonsalves
    Laurence Gonsalves over 12 years
    I suggest the name "zippend".
  • dhg
    dhg over 12 years
    @Laurence I like it! Edited!
  • ruffryder
    ruffryder over 12 years
    That looks pretty interesting. Can you explain the bounds of it a little more? I assume you could uses as many arrays and outputs as you wanted?
  • ruffryder
    ruffryder over 12 years
    I am curious how this method would work if the function returned an array, instead of a single value.
  • dhg
    dhg over 12 years
    @ruffryder Edited to address your first comment.
  • dhg
    dhg over 12 years
    @ruffryder Edited to address your second comment.