Access Functions in Dictionary Part 2
<p>As a follow-up to <a href="http://stackoverflow.com/questions/39926137/access-functions-via-dictionary">this question</a>:
I have 2 functions that look like this:</p>

<pre><code>def abc(a,b):
    return a+b

def cde(c,d):
    return c+d
</code></pre>

<p>And I want to assign it to a dictionary like this:</p>

<pre><code>functions = {'abc': abc(a,b), 'cde': cde(c,d)}
</code></pre>

<p>I could do this, but it would break at 'cde':</p>

<pre><code>functions = {'abc':abc, 'cde':cde}

functions_to_call = ['abc', 'cde']
for f in functions_to_call:
    a, b = 3, 4
    c, d = 1, 2
    if f in functions:
       functions[f](a, b)
</code></pre>

<p>Also, what if cde took 3 arguments? </p>

<p>Make a seperate sequence of <code>args</code> and use the splat operator (<code>*</code>):</p>

<pre><code>&gt;&gt;&gt; def ab(a,b):
...   return a + b
... 
&gt;&gt;&gt; def cde(c,d,e):
...   return c + d + e
... 
&gt;&gt;&gt; funcs = {'ab':ab, 'cde':cde}
&gt;&gt;&gt; to_call = ['ab','cde']
&gt;&gt;&gt; args = [(1,2),(3,4,5)]
&gt;&gt;&gt; for fs, arg in zip(to_call,args):
...   print(funcs[fs](*arg))
... 
3
12
</code></pre>

python list function python-3.x dictionary