Mapping sums of defaultdict(list) to one list
<p>I have a large collection of data formatted somewhat like the <code>d.items()</code> of a <code>defaultdict(list)</code>. See below:</p>

<pre><code>products = [(('blue'), ([2, 4, 2, 4, 2, 4, 2, 4, 2, 4], [2, 4, 2, 4, 2, 4, 2, 4, 2, 4], [2, 4, 2, 4, 2, 4, 2, 4, 2, 4])),
        (('yellow'), ([1, 3, 1, 3, 1, 3, 1, 3, 1, 3], [1, 3, 1, 3, 1, 3, 1, 3, 1, 3], [1, 3, 1, 3, 1, 3, 1, 3, 1, 3])),
        (('red'), ([1, 1, 1, 1, 1, 2, 5, 4, 6, 4], [2, 5, 3, 4, 8, 1, 1, 1, 1, 1], [8, 6, 3, 9, 2, 1, 1, 1, 1, 1]))]
</code></pre>

<p>And I want to map the sum of each data value in the nested lists to their corresponding counterparts in the same position or index to produce the final sums like follows:</p>

<pre><code>['blue', 6, 12, 6, 12, 6, 12, 6, 12, '6.000000', 12]
['yellow', 3, 9, 3, 9, 3, 9, 3, 9, '3.000000', 9]
['red', 11, 12, 7, 14, 11, 4, 7, 6, '8.000000', 6]
</code></pre>

<p>With loops, it can be done easily as shown in this function:</p>

<pre><code>def summation(products):

    sums = []

    for item in products:
            sums.append([(item[0]),
                     sum(int(x[0]) for x in item[1]),
                     sum(int(x[1]) for x in item[1]),
                     sum(int(x[2]) for x in item[1]),
                     sum(int(x[3]) for x in item[1]),
                     sum(int(x[4]) for x in item[1]),
                     sum(int(x[5]) for x in item[1]),
                     sum(int(x[6]) for x in item[1]),
                     sum(int(x[7]) for x in item[1]),
                     "{:.6f}".format(sum(float(x[8]) for x in item[1])),
                     sum(int(x[9]) for x in item[1])])

    for s in sums:
        print(s)
</code></pre>

<p>The problem arises when the size of products is millions, i.e. it is very time-consuming. So I thought of implementing mapping each value to its corresponding one in the nested list for the same key. This is what I tried:</p>

<pre><code>def mappingSum(products):
    sums = []

    for item in products:
        sums.append([item[0], map((sum(x), sum(y), sum(z)) for x, y, z in item[1])])



    for s in sums:
        print(s)
</code></pre>

<p>However, I get the following error:</p>

<pre><code>TypeError: map() must have at least two arguments.
</code></pre>

<p>I don't know how to resolve it and I am not sure whether <code>map</code> is the right tool to do my task.</p>

<p>From what I understand, you need to <em>zip</em> the sublists in the list and sum them up:</p>

<pre><code>&gt;&gt;&gt; sums = [(key, [sum(value) for value in zip(*values)]) for key, values in products]
&gt;&gt;&gt; for s in sums:
...     print(s)
... 
('blue', [6, 12, 6, 12, 6, 12, 6, 12, 6, 12])
('yellow', [3, 9, 3, 9, 3, 9, 3, 9, 3, 9])
('red', [11, 12, 7, 14, 11, 4, 7, 6, 8, 6])
</code></pre>

python list python-3.x mapping defaultdict