Add SUM of values of two LISTS into new LIST Add SUM of values of two LISTS into new LIST python python

Add SUM of values of two LISTS into new LIST


The zip function is useful here, used with a list comprehension.

[x + y for x, y in zip(first, second)]

If you have a list of lists (instead of just two lists):

lists_of_lists = [[1, 2, 3], [4, 5, 6]][sum(x) for x in zip(*lists_of_lists)]# -> [5, 7, 9]


From docs

import operatorlist(map(operator.add, first,second))


Default behavior in numpy is add componentwise

import numpy as npnp.add(first, second)

which outputs

array([7,9,11,13,15])