Sum of list without lowest and highest list integers (python)
<p>Trying to practice my list comprehension, but at this point my code is looking a little (too) long per line length comprehension: </p>

<pre><code>def sum_array(arr):
    return 0 if arr == None else sum(sorted(arr)[1:-1] for x in range(len(arr or [])-2)) 
</code></pre>

<p>Objective is to calculate sum of integers minus the min and max. If array is empty, <code>None</code>, or if only 1 element exists, the function should return 0.</p>

<p>I am receivingthe following</p>

<blockquote>
  <p>TypeError: unsupported operand type(s) for +: 'int' and 'list'</p>
</blockquote>

<p>Please advise!  </p>

<p>You get <code>TypeError</code> because you try to <code>sum</code> lists. Let me change your code a little bit:</p>

<pre><code> In[1]: arr = [1, 2, 3, 4, 5]
        [sorted(arr)[1:-1] for x in range(len(arr))]

Out[1]: [[2, 3, 4], [2, 3, 4], [2, 3, 4], [2, 3, 4], [2, 3, 4]]
</code></pre>

<p>I change your generator expression on list comprehension to show the idea. Really you generate a list on each implicit <code>next</code> call. A further consequence of this error is:</p>

<pre><code> In[2]: sum([[1],[2],[3]])       # will raise TypeError
Out[2]: TypeError: unsupported operand type(s) for +: 'int' and 'list'

 In[3]: sum([[1],[2],[3]], [])   # if you specify start, it will work
Out[3]: [1, 2, 3]
</code></pre>

<p>The signature of <code>sum</code> function is <code>sum(iterable[, start])</code>. Sums <em>start</em> and the <em>items of an iterable</em> from left to right and returns the total. <code>start</code> defaults to 0. The iterableâs items are normally numbers, and the start value is not allowed to be a string. In fist case you really try to sum <code>[1] + [2] + [3] + 0</code>, and because <code>+</code> between <code>list</code> and <code>int</code> is not defined, that's why you get TypeError. In the second case <code>[1] + [2] + [3] + []</code> this operation concatenates list and is perfectly valid.</p>

<p>And if you want review of your code and some feedback look at Stackexchange's <a href="http://codereview.stackexchange.com/">CodeReview</a> site.</p>

<p>You can try this, if you like <em>one-liners</em>. Here is also an assumption that a valid array is always instance of <code>&lt;class list&gt;</code>:</p>

<pre><code> In[4]: sum_array = lambda arr: sum(sorted(arr)[1:-1]) if isinstance(arr, list) else 0
        sum_array([4, 3, 8, 1, 7, 12, 5, 9])
Out[4]: 36
</code></pre>

<p><strong>But it's not good practice!</strong></p>

python if-statement list-comprehension