-1

I'm wondering if there's a way of getting multiple outputs from a function into a list. I'm not interested in creating a list inside of a function for reasons I'm not going to waste your time going into.

I know how many output variables I am expecting, but only through using the annotations["return"] expression (or whatever you call that, sorry for the noobish terminology) and this changes from case to case, which is why I need this to be dynamic.

I know I can use lists as multiple variables using function(*myList), but I'm interested in if there's a way of doing the equivalent when receiving return values from a function.

Cheers!

Pseudocode:

function():
   x = 1
   y = 2
   return x, y

variables = function()
print(variables[0], " and ", variables[1]

result should be = "1 and 2"
  • You mean unpacking a list (returned from a function) into multiple variables? A function can only return _one_ value. That value may be a list or tuple, but it's still just one thing. – gen_Eric May 11 '22 at 17:20
  • Can you show a (pseudo-code) example of what you are trying to do? – gen_Eric May 11 '22 at 17:20
  • 1
    @Rocket - So you're saying that when we write return "string1", "string2", it is actually returning it as a list? – FilterFeeder May 11 '22 at 17:27
  • 2
    Did you try to convert your pseudocode to real code? It should work the way you expect pretty much as-is. Returning multiple variables from your function implicitly returns a `tuple` containing all those values. You can index into a tuple directly using the same syntax as you do for a list, or convert a tuple to a list using the `list()` type. – Pranav Hosangadi May 11 '22 at 17:39
  • Does this answer your question? [Convert tuple to list and back](https://stackoverflow.com/questions/16296643/convert-tuple-to-list-and-back) – Pranav Hosangadi May 11 '22 at 17:41
  • @FilterFeeder Yeah `return x, y` is the same as `return (x, y)` which is a returning a tuple with 2 values. – gen_Eric May 11 '22 at 18:35

1 Answers1

1

yes, with the unpacking assignments expression ex a,b,c= myfunction(...), you can put * in one of those to make it take a variable number of arguments

>>> a,b,c=range(3) #if you know that the thing contains exactly 3 elements you can do this
>>> a,b,c
(0, 1, 2)
>>> a,b,*c=range(10) #for when you know that there at least 2 or more the first 2 will be in a and b, and whatever else in c which will be a list
>>> a,b,c
(0, 1, [2, 3, 4, 5, 6, 7, 8, 9])
>>> a,*b,c=range(10)
>>> a,b,c
(0, [1, 2, 3, 4, 5, 6, 7, 8], 9)
>>> *a,b,c=range(10)
>>> a,b,c
([0, 1, 2, 3, 4, 5, 6, 7], 8, 9)
>>> 

additionally you can return from a function whatever you want, a list, a tuple, a dict, etc, but only one thing

>>> def fun():
        return 1,"boo",[1,2,3],{1:10,3:23}

>>> fun()
(1, 'boo', [1, 2, 3], {1: 10, 3: 23})
>>> 

in this example it return a tuple with all that stuff because , is the tuple constructor, so it make a tuple first (your one thing) and return it

Copperfield
  • 8,131
  • 3
  • 23
  • 29
  • Brilliant! Also realizing that just returning multiple types, and stating that x = function() will yield a tuple containing the values, as you say. Was simply my python ignorance that was making this harder than I thought it was. Thanks! – FilterFeeder May 11 '22 at 17:45