Sum digits of numbers in a list
<p>Trying to write a function that takes a list like:</p>

<pre><code>x = [1, 13, 14, 9, 8]
</code></pre>

<p>for example and sums the digits like:</p>

<pre><code>1 + (1+3) + (1+4) + 9 + 8 = 27       
</code></pre>

<p>What I have attempted thus far:</p>

<pre><code>def sum_d(x):
    if not x:
        return 0
    else:
        return x[0] + sum_d(x[1:])
</code></pre>

<p>A slight modification, and your original code will work :</p>

<pre><code>def sum_d(x):
    x = str(x)
    if len(x) == 1:
        return int(x)
    else:
        return int(x[0]) + sum_d(int(x[1:]))
</code></pre>

<p>also possible in single line :</p>

<pre><code>sum(int(y) for y in (chain(*[str(x) for x in [1, 13, 14, 9, 8]])))
</code></pre>

<p>Explained here :</p>

<pre><code>&gt;&gt;&gt; from itertools import chain
&gt;&gt;&gt; [str(x) for x in [1, 13, 14, 9, 8]]
['1', '13', '14', '9', '8']
&gt;&gt;&gt; chain(*[str(x) for x in [1, 13, 14, 9, 8]])
&lt;itertools.chain object at 0x7feadcd067d0&gt;
&gt;&gt;&gt; list(chain(*[str(x) for x in [1, 13, 14, 9, 8]]))
['1', '1', '3', '1', '4', '9', '8']
&gt;&gt;&gt; sum(int(y) for y in (chain(*[str(x) for x in [1, 13, 14, 9, 8]])))
27
</code></pre>

python list python-3.x