Append output function to multiple lists
<p>I want to execute a function with different parameter values. I have the following snippet of code which works perfectly well:</p>

<pre><code>tau = np.arange(2,4.01,0.1)
R = []
P = []
T = []
L = []
D = []
E = []
Obj = []
for i, tenum in enumerate(tau):
    [r, p, t, l, d, e, obj] = (foo.cvxEDA(edaN, 1./fs, tenum, 0.7, 10.0, 0.0008, 0.01))
    R.append(r)
    P.append(p)
    T.append(t)
    L.append(l)
    D.append(d)
    E.append(e)
    Obj.append(obj)
</code></pre>

<p>However, I was wondering though: <strong>Is there an easier way to accomplish this?</strong> </p>

<hr>

<p>I have tried using 
<code>res.append(foo.cvxEDA(edaN, 1./fs, tenum, 0.7, 10.0, 0.0008, 0.01)</code> but <code>res[1]</code> returns <code>&lt;generator object &lt;genexpr&gt; at 0x046E7698&gt;</code>. </p>

<p>You can turn a generator object into a list object by just passing it to the <code>list()</code> function so maybe this will do what you want:</p>

<pre><code>res = []
for i, tenum in enumerate(tau):
   res.append(list(foo.cvxEDA(edaN, 1./fs, tenum, 0.7, 10.0, 0.0008, 0.01)))
</code></pre>

<p>Even shorter with a list comprehension:</p>

<pre><code>res = [list(foo.cvxEDA(edaN, 1./fs, tenum, 0.7, 10.0, 0.0008, 0.01)) for i, tenum in enumerate(tau)]
</code></pre>

<p>Either way, this leaves res transposed compared to what you want (thinking of it as a matrix). You can fix that with a call to <code>zip</code>:</p>

<pre><code>res_tr = zip(*res)
R, P, T, L, D, E, Obj = res_tr
</code></pre>

<p>Edit: Shortest of all, you can avoid building the intermediate list with a generator expression passed directly to <code>zip()</code>:</p>

<pre><code>R, P, T, L, D, E, Obj = zip(*(list(foo.cvxEDA(edaN, 1./fs, tenum, 0.7, 10.0, 0.0008, 0.01)) for tenum in tau))
</code></pre>

<p>One final note: In all of these, you can replace "<code>for i, tenum in enumerate(tau)</code>" with "<code>for tenum in tau</code>" since you don't seem to be using <code>i</code>.</p>

python list python-2.7 append