Positive Count // Negative Sum
<p>A fairly easy problem, but I'm still practicing iterating over multiple variables with for loops. In the below, I seek to return a new list, where x is the count of positive numbers and y is the sum of negative numbers from an input array <code>arr.</code></p>

<p>If the input array is empty or null, I am to return an empty array. </p>

<p>Here's what I've got! </p>

<pre><code>def count_positives_sum_negatives(arr):
    return [] if not arr else [(count(x), sum(y)) for x, y in arr]
</code></pre>

<p>Currently receiving... </p>

<p>TypeError: 'int' object is not iterable</p>

<p>the error you get is from this part <code>for x,y in arr</code> that mean that <code>arr</code> is expected to be a list of tuples of 2 elements (or any similar container), like for example this <code>[(1,2), (5,7), (7,9)]</code> but what you have is a list of numbers, which don't contain anything else inside... </p>

<p>Now to get your desire result you can use the solution of <strong>wim</strong>, which need to iterate over the list twice or you can get it in one go with</p>

<pre><code>&gt;&gt;&gt; def fun(iterable):
        if not iterable:
            return []
        pos = 0
        neg = 0
        for n in iterable:
            if n&gt;=0:
                pos = pos + 1
            else:
                neg = neg + n
        return [pos, neg]

&gt;&gt;&gt; arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15]
&gt;&gt;&gt; fun(arr)
[10, -65]
&gt;&gt;&gt; 
</code></pre>

python list-comprehension multiple-variable-return