Sum of List of Lists; Returns Sum List

Sum of list of lists; returns sum list

You could try this:

In [9]: l = [[3,7,2],[1,4,5],[9,8,7]]

In [10]: [sum(i) for i in zip(*l)]
Out[10]: [13, 19, 14]

This uses a combination of zip and * to unpack the list and then zip the items according to their index. You then use a list comprehension to iterate through the groups of similar indices, summing them and returning in their 'original' position.

To hopefully make it a bit more clear, here is what happens when you iterate through zip(*l):

In [13]: for i in zip(*l):
....: print i
....:
....:
(3, 1, 9)
(7, 4, 8)
(2, 5, 7)

In the case of lists that are of unequal length, you can use itertools.izip_longest with a fillvalue of 0 - this basically fills missing indices with 0, allowing you to sum all 'columns':

In [1]: import itertools

In [2]: l = [[3,7,2],[1,4],[9,8,7,10]]

In [3]: [sum(i) for i in itertools.izip_longest(*l, fillvalue=0)]
Out[3]: [13, 19, 9, 10]

In this case, here is what iterating over izip_longest would look like:

In [4]: for i in itertools.izip_longest(*l, fillvalue=0):
...: print i
...:
(3, 1, 9)
(7, 4, 8)
(2, 0, 7)
(0, 0, 10)

sum for list of lists

You can use sum() with a generator expression here:

In [18]: lis = [[1, 2], [3, 4], [5, 6]]

In [19]: sum(sum(x) for x in lis)
Out[19]: 21

or:

In [21]: sum(sum(lis, []))
Out[21]: 21

timeit comparisons:

In [49]: %timeit sum(sum(x) for x in lis)
100000 loops, best of 3: 2.56 us per loop

In [50]: %timeit sum(map(sum, lis))
100000 loops, best of 3: 2.39 us per loop

In [51]: %timeit sum(sum(lis, []))
1000000 loops, best of 3: 2.21 us per loop

In [52]: %timeit sum(chain.from_iterable(lis)) # winner
100000 loops, best of 3: 1.43 us per loop

In [53]: %timeit sum(chain(*lis))
100000 loops, best of 3: 1.55 us per loop

Summing elements of list of lists in Python

A very simple way to solve this, is to maintain a total variable and replace the second value of each sub-list with this sum.

myList = [[8100, 3], [8200, 5], [8400, 8]]
total, new_list = 0, []

for sublist in myList:
total += sublist[1]
new_list.append([sublist[0],total])

print(new_list)

Output

[[8100, 3], [8200, 8], [8400, 16]]

Performing a row sum and column sum on a list of lists in python

colsum = sum(row[0] for row in matrix)

As a note for others who look at this question though, this really is a task best left to numpy. OP is not allowed external libraries however.

Summing a list of lists

Here's the help from the REPL:

>>> help(sum)

sum(iterable, start=0, /)
Return the sum of a 'start' value (default: 0) plus an iterable of numbers

When the iterable is empty, return the start value.
This function is intended specifically for use with numeric values and may
reject non-numeric types.

So, the sum built-in returns the sum of the start value, i.e. 0, and an iterable of numbers. Python doesn't prevent you from misusing a function beforehand, it trusts that you are at least trying to do the right thing. Of course, if you happen to pass a list-of-lists, the first list element will be summed with 0, raising:

TypeError: unsupported operand type(s) for +: 'int' and 'list'

Indeed, if you pass a start argument, an empty list, in this case, it works:

>>> sum([[e] for e in x], [])
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

However, this will be inefficient. You should prefer [x for sublist in list_of_lists for x in sublist] or any other linear time algorithm.

Sum integers from a list of lists

Recursion means you have to use a function.

tab = [7, 5, [3, 6, [2]], 7, [1, [2, 3, [4]], 9, 2], 4]

def int_sum(tbl):
s = 0
for e in tbl:
if isinstance(e, int):
s += e
else:
# this is the trick!
s += int_sum(e)
return s

print(int_sum(tab))


Related Topics



Leave a reply



Submit