Retruning multiple objects and using them as arguments
<p>I have a function which goes something like this:</p>

<pre><code>def do_something(lis):
    do something
    return lis[0], lis[1]
</code></pre>

<p>and another function which needs to take those two return objects as arguments:</p>

<pre><code>def other_function(arg1, arg2):
    pass
</code></pre>

<p>I have tried:</p>

<pre><code>other_function(do_something(lis))
</code></pre>

<p>but incurred this error:</p>

<pre><code>TypeError: other_function() missing 1 required positional argument: 'arg2'
</code></pre>

<p>You need to unpack those arguments when calling <code>other_function</code>. </p>

<pre><code>other_function(*do_something(lis))
</code></pre>

<p>Based on the error message, it looks like your other function is defined (and should be defined as)</p>

<pre><code>def other_function(arg1, arg2):
    pass
</code></pre>

<p>So, when you return from <code>do_something</code>, you are actually returning a tuple containing <code>(lis[0], lis[1])</code>. So when you originally called <code>other_function</code>, you passing a single tuple, when your <code>other_function</code> was still expecting a second argument. </p>

<p>You can see this if you break it down a bit further. Below is a breakdown of how the returns look like when handled differently, a reproduction of your error and a demo of the solution: </p>

<p>Returning in to a single variable will return a tuple of the result:</p>

<pre><code>&gt;&gt;&gt; def foo():
...     lis = range(10)
...     return lis[1], lis[2]
...
&gt;&gt;&gt; result = foo()
&gt;&gt;&gt; result
(1, 2)
</code></pre>

<p>Returning in to two variables, unpacks in to each var:</p>

<pre><code>&gt;&gt;&gt; res1, res2 = foo()
&gt;&gt;&gt; res1
1
&gt;&gt;&gt; res2
2
</code></pre>

<p>Trying to call other_function with <code>result</code> which now only holds a tuple of your result:</p>

<pre><code>&gt;&gt;&gt; def other_function(arg1, arg2):
...     print(arg1, arg2)
...
&gt;&gt;&gt; other_function(result)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
TypeError: other_function() missing 1 required positional argument: 'arg2'
</code></pre>

<p>Calling other_function with <code>res1</code>, <code>res2</code> that holds each value of the return from <code>foo</code>:</p>

<pre><code>&gt;&gt;&gt; other_function(res1, res2)
1 2
</code></pre>

<p>Using <code>result</code> (your tuple result) and unpacking in your function call to <code>other_function</code>:</p>

<pre><code>&gt;&gt;&gt; other_function(*result)
1 2
</code></pre>

python function functional-programming return arguments