# Sum of list of lists; returns sum list

You could try this:

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

In : [sum(i) for i in zip(*l)]
Out: [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 : 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 : import itertools

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

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

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

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