Use map with operator.add:

>>> from operator import add
>>> list( map(add, list1, list2) )
[5, 7, 9]

or zip with a list comprehension:

>>> [sum(x) for x in zip(list1, list2)]
[5, 7, 9]

Timing comparisons:

>>> list2 = [4, 5, 6]*10**5
>>> list1 = [1, 2, 3]*10**5
>>> %timeit from operator import add;map(add, list1, list2)
10 loops, best of 3: 44.6 ms per loop
>>> %timeit from itertools import izip; [a + b for a, b in izip(list1, list2)]
10 loops, best of 3: 71 ms per loop
>>> %timeit [a + b for a, b in zip(list1, list2)]
10 loops, best of 3: 112 ms per loop
>>> %timeit from itertools import izip;[sum(x) for x in izip(list1, list2)]
1 loops, best of 3: 139 ms per loop
>>> %timeit [sum(x) for x in zip(list1, list2)]
1 loops, best of 3: 177 ms per loop
Answer from Ashwini Chaudhary on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › element-wise sum of unknown number of lists
r/learnpython on Reddit: Element-wise sum of unknown number of lists
May 13, 2021 -

My data is structured in a Python dictionary as:

dict = {
    'key1': {
        'data1': [10, 20, 30, 40, 50]
    },
    'key2': {
        'data2': [60, 70, 80, 90, 100]
    },
    'key3': {
        'data3': [110, 120, 130, 140, 150]
    }
}

where the three lists have the same number of elements.

Now, I'd like to sum, in a element-wise manner, the three "dataX" lists, in order to obtain a single list.

In this example, there are three keys (and, then, three lists), but they could also be one, two, or four.

Which is a good way to achieve this goal?

Adding lists together Sep 12, 2020
r/learnpython
6y ago
[Question] NumPy array element-wise sum Apr 24, 2021
r/learnpython
5y ago
Sum of Lists (Python) Jan 17, 2021
r/learnpython
5y ago
Getting the sum of a list with python? Oct 19, 2020
r/AskProgramming
5y ago
More results from reddit.com
🌐
The Renegade Coder
therenegadecoder.com › code › how-to-sum-elements-of-two-lists-in-python
How to Sum Elements of Two Lists in Python: Comprehensions and More – The Renegade Coder
May 27, 2024 - In short, one of the best ways to sum elements of two lists in Python is to use a list comprehension in conjunction with the addition operator. For example, we could perform an element-wise sum of two lists as follows: [x + y for x, y in zip(list_a, ...
Discussions

python - Find the sum of two lists of lists element-wise - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
sum of N lists element-wise python - Stack Overflow
Is there an easy way to compute the element-wise sum of N lists in python? I know if we have n lists defined (call the ith list c_i), we can do: z = [sum(x) for x in zip(c_1, c_2, ...)] For exampl... More on stackoverflow.com
🌐 stackoverflow.com
October 9, 2013
python - Element-wise sum of nested lists - Stack Overflow
I have a nested list called list_6: [[-1, 1, -1, 1, -1, 1, 1, 0, -1, 0, -1, -1, 1, 1, 1, 0, 1, -1, -1, -1, 1, 1, 1, 0, 0, -1, 0, 0, 0, 1, 0, -1, 1, 1, -1, 0, 0, 1, 1, -1, 0, -1, 1, 1, -1, 1, -1, -1... More on stackoverflow.com
🌐 stackoverflow.com
Sum element by element list of lists python - Stack Overflow
I am experiencing some issues with the following task: I have a list of lists. I have called "weighted_prob" the list of lists, where each element of "weighted_prob" is called "weighted_variable... More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 16
484

Use map with operator.add:

>>> from operator import add
>>> list( map(add, list1, list2) )
[5, 7, 9]

or zip with a list comprehension:

>>> [sum(x) for x in zip(list1, list2)]
[5, 7, 9]

Timing comparisons:

>>> list2 = [4, 5, 6]*10**5
>>> list1 = [1, 2, 3]*10**5
>>> %timeit from operator import add;map(add, list1, list2)
10 loops, best of 3: 44.6 ms per loop
>>> %timeit from itertools import izip; [a + b for a, b in izip(list1, list2)]
10 loops, best of 3: 71 ms per loop
>>> %timeit [a + b for a, b in zip(list1, list2)]
10 loops, best of 3: 112 ms per loop
>>> %timeit from itertools import izip;[sum(x) for x in izip(list1, list2)]
1 loops, best of 3: 139 ms per loop
>>> %timeit [sum(x) for x in zip(list1, list2)]
1 loops, best of 3: 177 ms per loop
2 of 16
139

The others gave examples how to do this in pure python. If you want to do this with arrays with 100.000 elements, you should use numpy:

In [1]: import numpy as np
In [2]: vector1 = np.array([1, 2, 3])
In [3]: vector2 = np.array([4, 5, 6])

Doing the element-wise addition is now as trivial as

In [4]: sum_vector = vector1 + vector2
In [5]: print sum_vector
[5 7 9]

just like in Matlab.

Timing to compare with Ashwini's fastest version:

In [16]: from operator import add
In [17]: n = 10**5
In [18]: vector2 = np.tile([4,5,6], n)
In [19]: vector1 = np.tile([1,2,3], n)
In [20]: list1 = [1,2,3]*n
In [21]: list2 = [4,5,6]*n
In [22]: timeit map(add, list1, list2)
10 loops, best of 3: 26.9 ms per loop

In [23]: timeit vector1 + vector2
1000 loops, best of 3: 1.06 ms per loop

So this is a factor 25 faster! But use what suits your situation. For a simple program, you probably don't want to install numpy, so use standard python (and I find Henry's version the most Pythonic one). If you are into serious number crunching, let numpy do the heavy lifting. For the speed freaks: it seems that the numpy solution is faster starting around n = 8.

Top answer
1 of 3
2

You could just use zip like,

>>> list1
[[1, 2, 3], [4, 5, 6]]
>>> list2
[[10, 2, 3], [11, 5, 6]]
>>> [[x+y for x,y in zip(l1, l2)] for l1,l2 in zip(list1,list2)]
[[11, 4, 6], [15, 10, 12]]

or if you are not sure, if both list will be of same length, then you can use zip_longest (izip_longest in python2) from itertools and use the fillvalue like,

>>> import itertools
>>> y = itertools.zip_longest([1,2], [3,4,5], fillvalue=0)
>>> list(y)
[(1, 3), (2, 4), (0, 5)]

that then you can use it for unequal sized data like,

>>> from itertools import zip_longest
>>> list1=[[1, 2, 3], [4, 5]]
>>> list2=[[10, 2, 3], [11, 5, 6], [1,2,3]]
>>> [[x+y for x,y in zip_longest(l1, l2, fillvalue=0)] for l1,l2 in zip_longest(list1,list2, fillvalue=[])]
[[11, 4, 6], [15, 10, 6], [1, 2, 3]]
2 of 3
1

In Python, it's rarely necessary to use indices, particularly for a task like this where you want to do the same thing to each element individually. The main function for that sort of transformation is map, with list comprehensions offering a convenient shorthand. However, map does one thing those don't - process multiple iterables in parallel, like Haskell's zipWith. We can break this down in two stages to do that:

list1 = [[1, 2, 3], [4, 5, 6]]
list2 = [[10, 2, 3], [11, 5, 6]]
from operator import add
def addvectors(a, b):
    return list(map(add, a, b))
list3 = list(map(addvectors, list1, list2))

In Python 2, map returns a list, so you don't need to separately collect it as I've done with list here.

🌐
Finxter
blog.finxter.com › home › learn python blog › how to add two lists element-wise in python
How to Add Two Lists Element-wise in Python - Be on the Right Side of Change
May 29, 2022 - Summary: The most pythonic approach to add two lists element-wise is to use zip() to pair the elements at the same positions in both lists and then add the two elements. Here’s a quick look at the solution: [x + y for x, y in zip(li_1, li_2)]. ...
🌐
Finxter
blog.finxter.com › 5-best-ways-to-sum-lists-of-tuples-element-wise-in-python
5 Best Ways to Sum Lists of Tuples Element-Wise in Python – Be on the Right Side of Change
This method involves initializing a tuple of sums to zero and then iterating over each tuple in the list, adding the elements to the corresponding sum in a straightforward manner. This method is easy to understand and implement for beginners. ... list_of_tuples = [(1, 2), (3, 4), (5, 6)] sums ...
Find elsewhere
🌐
DEV Community
dev.to › therenegadecoder › how-to-sum-elements-of-two-lists-in-python-17c7
Python Sum of List : How to Sum Elements of Two Lists in Python - DEV Community
September 5, 2019 - Just like the last lesson on inverting dictionaries in Python, we can take advantage of comprehensions to dramatically simplify this problem. Let’s take a look: ethernet_devices = [1, [7], [2], [8374163], [84302738]] usb_devices = [1, [7], [1], [2314567], [0]] all_devices = [x + y for x, y in zip(ethernet_devices, usb_devices)] Now we’re talking! That’s five lines of tedious mapping compressed down into a simple list comprehension. But wait, it gets better: all_devices = [sum(pair) for pair in zip(ethernet_devices, usb_devices)]
🌐
GeeksforGeeks
geeksforgeeks.org › python-ways-to-sum-list-of-lists-and-return-sum-list
Python - Ways to sum list of lists and return sum list - GeeksforGeeks
December 18, 2024 - A result list sum_list is initialized with zeros. A nested loop iterates through each list and its elements, adding corresponding values. ... The task of concatenate two lists of lists row-wise, meaning we merge corresponding sublists into a single sublist.
🌐
Delft Stack
delftstack.com › home › howto › python › element wise addition python
How to Perform Element-Wise Addition in Python | Delft Stack
February 12, 2024 - Executing element-wise addition using list comprehension and the zip() function in Python involves creating a new list by adding corresponding elements from two or more lists. Again, the zip() function pairs up the elements at the same positions in multiple lists, and then list comprehension is used to iterate through these pairs and generate a new list containing the sums...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-adding-two-list-elements
Add Elements of Two Lists in Python - GeeksforGeeks
July 11, 2025 - The most basic way to add corresponding elements is by using a simple for loop. This method is easy to understand for beginners. ... # Input lists a = [1, 3, 4, 6, 8] b = [4, 5, 6, 2, 10] # Create an empty list to store the result c = [] # Loop ...
🌐
Data Science Dojo
discuss.datasciencedojo.com › python
How can I perform an element-wise addition of two lists in Python? - Python - Data Science Dojo Discussions
May 10, 2023 - I’m trying to add two lists element-wise in Python, but I’m not sure what the best way to do it is. Can anyone help me with this? Here’s what I’ve tried so far: list1 = [1, 2, 3] list2 = [4, 5, 6] result = list1 + list2 However, this just concatenates the two lists together, resulting in [1, 2, 3, 4, 5, 6]. I want to get the result [5, 7, 9], which is the element-wise sum of list1 and list2.
🌐
IQCode
iqcode.com › code › python › python-sum-lists-element-wise
python sum lists element wise Code Example
October 9, 2021 - import numpy as np vector1 = np.array([1, 2, 3]) vector2 = np.array([4, 5, 6]) #Doing the element-wise addition is now as trivial as: sum_vector = vector1 + vector2 print(sum_vector) # [5 7 9] ... Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
🌐
Educative
educative.io › answers › how-to-compute-the-sum-of-a-list-in-python
How to compute the sum of a list in python
The sum() function in Python cannot directly sum elements of a nested list (a list within a list), as it only works with flat iterables.
🌐
PyTorch Forums
discuss.pytorch.org › t › element-wise-sum-of-batched-tensors › 100988
Element wise sum of batched tensors - PyTorch Forums
October 29, 2020 - I have a list of tensors t_list and I need to get the element wise sum. I am concerned that because the tensors are in a batch that my method is incorrect. Code example t_list = [t1, t2, t3, t4] #where ti is a tensor 32 x 1 x 128 t_list = torch.stack(t_list) # giving 4 x 32 x 1 x 128 sum_list = sum(t_list) # result is 1 x 32 x 1 x 128 Is this the correct way to sum each list within the batch or would I need to specify dimensions for sum?
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python add two lists by index wise
Python Add Two Lists By Index Wise - Spark By {Examples}
May 31, 2024 - How to add two lists in python, for example, index-wise summation of numbers? You can perform index-wise addition or summation of two lists by using list