Coder Perfect

Adding two lists element by element?

Problem

I have now:

list1 = [1, 2, 3]
list2 = [4, 5, 6]

I would like to have:

[1, 2, 3]
 +  +  +
[4, 5, 6]
|| || ||
[5, 7, 9]

It’s just a simple element-by-element addition of two lists.

I could iterate the two lists, but I’m not interested in doing so.

What is the most Pythonic approach to this?

Asked by Sibbs Gambling

Solution #1

Use the operator with the map. add:

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

or a comprehension of a list:

>>> [sum(x) for x in zip(list1, list2)]
[5, 7, 9]
>>> 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

Answered by Ashwini Chaudhary

Solution #2

Others demonstrated how to do it in pure Python. Numpy should be used if you wish to perform this with arrays of 100.000 elements:

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

The element-by-element addition is now as simple as it gets.

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

in the same way that Matlab does.

To compare with Ashwini’s fastest version, here’s the time:

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

As a result, this is a factor of 25 faster! However, utilize whatever works best for you. You probably don’t want to install numpy for a basic program, so stick with normal Python (and Henry’s version is the most Pythonic). Allow numpy to perform the heavy lifting if you’re into serious number crunching. For speed enthusiasts, starting around n = 8, the numpy solution appears to be faster.

Answered by Bas Swinckels

Solution #3

[a + b for a, b in zip(list1, list2)]

Answered by Henry Gomersall

Solution #4

As described by others, a fast and also space efficient solution is using numpy (np) with it’s built-in vector manipulation capability:

1. With Numpy

x = np.array([1,2,3])
y = np.array([2,3,4])
print x+y

2. With built-ins

2.1 Lambda

list1=[1, 2, 3]
list2=[4, 5, 6]
print map(lambda x,y:x+y, list1, list2)

It’s worth noting that map() accepts several arguments.

2.2 understanding of zip files and lists

list1=[1, 2, 3]
list2=[4, 5, 6]
print [x + y for x, y in zip(list1, list2)]

Answered by MasterControlProgram

Solution #5

Numpy is, in my perspective, easier to use:

import numpy as np
list1=[1,2,3]
list2=[4,5,6]
np.add(list1,list2)

Results:

Check out numpy.add for further information on the parameters.

Answered by Ludwig Zhou

Post is based on https://stackoverflow.com/questions/18713321/element-wise-addition-of-2-lists