9

I know that it is possible for a function to return multiple values in Python. What I would like to do is return each element in a list as a separate return value. This could be an arbitrary number of elements, depending on user input. I am wondering if there is a pythonic way of doing so?

For example, I have a function that will return a pair of items as an array, e.g., it will return [a, b].

However, depending on the input given, the function may produce multiple pairs, which will result in the function returning [[a, b], [c, d], [e, f]]. Instead, I would like it to return [a, b], [c, d], [e, f]

As of now, I have implemented a very shoddy function with lots of temporary variables and counts, and am looking for a cleaner suggestion.

Appreciate the help!

luisamaria
  • 105
  • 1
  • 1
  • 5
  • 2
    It's not entirely true to say the python functions can return more than one value: such functions actually return a tuple, which is unpacked on assignment. So you can simply convert your list to a tuple using `tuple()`. If what you mean is that you want to return them one-at-a-time on multiple calls, well then you're using `yield` and building a generator, something else entirely. – Lee Daniel Crocker Dec 03 '14 at 00:24
  • 1
    It's worth noting tuples are only the norm when it comes to 'returning multiple values'. You can happily return a list and unpack it just like a tuple. – Gareth Latty Dec 03 '14 at 00:35

5 Answers5

12

When a python function executes:

return a, b, c

what it actually returns is the tuple (a, b, c), and tuples are unpacked on assignment, so you can say:

x, y, z = f()

and all is well. So if you have a list

mylist = [4, "g", [1, 7], 9]

Your function can simply:

return tuple(mylist)

and behave like you expect:

num1, str1, lst1, num2 = f()

will do the assignments as you expect.

If what you really want is for a function to return an indeterminate number of things as a sequence that you can iterate over, then you'll want to make it a generator using yield, but that's a different ball of wax.

Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
10

There is a yield statement which matches perfectly for this usecase.

def foo(a):
    for b in a:
        yield b

This will return a generator which you can iterate.

print [b for b in foo([[a, b], [c, d], [e, f]])
wenzul
  • 3,948
  • 2
  • 21
  • 33
2

The answer is very simple just join the elements of list by providing a required separator

Syntax for join method :

"separator".join(iterable)

For example let us consider a function where you need to return all the list values separated by a comma (,)

def fun(samp_list):
    return ",".join(samp_list)

print(fun(["1","2","3"]))

And the answer as follows

1,2,3

Example 2 : Return list values separated by a new line

def fun(samp_list):
   return "/n".join(samp_list)

print(fun(["1","2","3"]))

And the output for this function is as follows

1
2
3

Hope it's useful

Do support by upvoting !

  • 1
    Welcome on stack overflow, I think the OP is looking for a tuple of python variable. with the `join` function output is a string and cannot be used afterward – Pierrick Rambaud Jan 08 '21 at 15:49
0

Check out this question: How to return multiple values from *args?

The important idea is return values, as long as they're a container, can be expanded into individual variables.

Community
  • 1
  • 1
matthewatabet
  • 1,463
  • 11
  • 26
0

However, depending on the input given, the function may produce multiple pairs, which will result in the function returning [[a, b], [c, d], [e, f]]. Instead, I would like it to return [a, b], [c, d], [e, f]

Can you not just use the returned list (i.e. the list [[a, b], [c, d], [e, f]]) and extract the elements from it? Seems to meet your criteria of arbitrary number of / multiple values.

akrishnamo
  • 449
  • 1
  • 3
  • 15
  • You can just use `tuple()` to convert the list to a tuple. Python multi-valued functions actually return a tuple, which is automatically unpacked on assignment. – Lee Daniel Crocker Dec 03 '14 at 00:26
  • I ended up doing something quite like this, based on the functionality of the caller method. But @wenzul suggestion is likely more pythonic, and I may rewrite it to use the more concise `yield`. Thanks guys! – luisamaria Dec 03 '14 at 16:57