In itertools
there is a variant of zip
which, when combining lists of different sizes, does not discard the extra elements, but uses None
for its values:
>>> teste = [[1,2,3],[4,5],[6,7,8,9]]
>>> list(izip_longest(*teste))
[(1, 4, 6), (2, 5, 7), (3, None, 8), (None, None, 9)]
Credit
Or, if you specified a default value via fillvalue
, this value is used (good for assigning zero and not interfering with sum):
>>> teste = [[1,2,3],[4,5],[6,7,8,9]]
>>> list(izip_longest(*teste, fillvalue=0))
[(1, 4, 6), (2, 5, 7), (3, 0, 8), (0, 0, 9)]
So you can use some list comprehensions to get the list of sums in a single command:
>>> x= [(y, [1, 2, 3]), (y, [4, 5]), (y, [6, 7, 8, 9])]
>>> somas = [sum(numeros) for numeros in
... izip_longest(*[segundo for primeiro,segundo in x], fillvalue=0)
... ]
>>> somas
[11, 14, 11, 9]