Python ProgrammingPython Programming

How to add two list items element-wise in Python?

Example-1

from itertools import zip_longest


thelist1 = [1, 4, 2, 3, 5, 4]
thelist2 = [3, 4, 5]

sumoflist = [sum(x) for x in zip_longest(thelist1, thelist2, fillvalue=0)]
print(sumoflist)
[4, 8, 7, 3, 5, 4]

Example-2

from operator import add


thelist1 = [1, 4, 2, 3, 5, 4]
thelist2 = [3, 4, 5]

sumoflist = list(map(add, thelist1, thelist2))
print(sumoflist)
[4, 8, 7]

Example-3

thelist1 = [1, 4, 2, 3, 5, 4]
thelist2 = [3, 4, 5]

sumoflist = [sum(i) for i in zip(thelist1, thelist2)]
print(sumoflist)
[4, 8, 7]