How to perform element-wise addition of different length lists? Assuming \"0\" for missing elements. Note: len(a) will always be less than or equal to len(b)
exa
You can use izip_longest:
izip_longest
>>> izip_longest(a,b,fillvalue=0) >>> list(_) [(1, 1), (2, 2), (3, 3), (0, 4), (0, 5)]
Then you can do:
>>> [sum(t) for t in izip_longest(a,b,fillvalue=0)] [2, 4, 6, 4, 5]