338

I have now:

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

I wish to have:

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

Simply an element-wise addition of two lists.

I can surely iterate the two lists, but I don't want do that.

What is the most Pythonic way of doing so?

martineau
  • 119,623
  • 25
  • 170
  • 301
Sibbs Gambling
  • 19,274
  • 42
  • 103
  • 174
  • Possible duplicate of [Concise vector adding in Python?](http://stackoverflow.com/questions/845112/concise-vector-adding-in-python) – Nikos Alexandris Aug 07 '16 at 15:20

17 Answers17

470

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
Utku
  • 2,025
  • 22
  • 42
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • 17
    If you use those huge arrays, the numpy solution by @BasSwinckels is probably something you should be looking at. – Henry Gomersall Sep 10 '13 at 08:34
  • 1
    What Python version did you use for those timings? – arshajii Sep 16 '13 at 00:20
  • 11
    N.B. - in python3, map() returns an iterable thingy rather than a list. If you do need an actual list, the first answer is list(map(add, list1, list2)) – FLHerne Mar 24 '16 at 11:25
  • Noting the python3 issue noted by @FLHerne with `map` will just grow more important over time. Python 2 will lose official support in less than 3 years. – nealmcb Apr 10 '17 at 00:29
  • 1
    There are many times where python syntax is really elegant and simple, but unfortunately this isn't one of them. And for such a simple task, it's a pity.... Why would they make "+" concatenate the lists when there's already the .extend() method? – Nic Scozzaro Nov 13 '18 at 02:37
  • How would one do this if they had more than two lists? Say 10 lists for example? – DS-V Feb 08 '20 at 04:54
  • just to note: `itertools` no longer has `izip`, and default `zip` is now basically a copy of the old `itertools.izip` https://stackoverflow.com/a/32659581/1676393 – postylem Apr 20 '23 at 14:31
  • You could also use `zip` and `sum` with `map` like this: `list(map(sum, zip(list1, list2)))` – wjandrea Jul 17 '23 at 16:27
  • @DS-V You can add arguments to `zip`, like `zip(list1, list2, list3)` – wjandrea Jul 17 '23 at 16:30
130

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.

Bas Swinckels
  • 18,095
  • 3
  • 45
  • 62
80
[a + b for a, b in zip(list1, list2)]
Henry Gomersall
  • 8,434
  • 3
  • 31
  • 54
  • 6
    @deltab The accepted answer is faster AND it contains this answer (more informative) – Sibbs Gambling Sep 11 '13 at 10:02
  • 4
    @perfectionm1ng though I understand your point (and don't begrudge it one bit) I just thought it's worth pointing out that I would always use either the solution I've presented (which given it requires no imports is arguably the simplest, as well as being arguably the more pythonic), or where speed counts, the answer of [Bas Swinckel](http://stackoverflow.com/a/18713494/709852), which is overwhelmingly the right option where speed matters. – Henry Gomersall Sep 11 '13 at 10:41
  • Yes. Thanks for the opinion. But essentially `[sum(x) for x in zip(list1, list2)]` is the same as your answer, isn't it? :) – Sibbs Gambling Sep 11 '13 at 10:55
  • 5
    @perfectionm1ng More or less (though it was added after mine with an edit :). Personally, I prefer the the a+b notation with explicit tuple unpacking for readability and pythonicness. – Henry Gomersall Sep 11 '13 at 11:01
16

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)

Notice that map() supports multiple arguments.

2.2 zip and list comprehension

list1=[1, 2, 3]
list2=[4, 5, 6]
print [x + y for x, y in zip(list1, list2)]
Mohammad Yusuf
  • 16,554
  • 10
  • 50
  • 78
  • 1
    +1 for the lambda approach. It's a shame this solution is combined with other solutions which are duplicated elsewhere. – LondonRob Jan 20 '19 at 19:14
15

It's simpler to use numpy from my opinion:

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

Results:

Terminal execution

For detailed parameter information, check here: numpy.add

Ludwig Zhou
  • 1,026
  • 1
  • 11
  • 23
9

Perhaps this is pythonic and slightly useful if you have an unknown number of lists, and without importing anything.

As long as the lists are of the same length, you can use the below function.

Here the *args accepts a variable number of list arguments (but only sums the same number of elements in each).

The * is used again in the returned list to unpack the elements in each of the lists.

def sum_lists(*args):
    return list(map(sum, zip(*args)))

a = [1,2,3]
b = [1,2,3]  

sum_lists(a,b)

Output:

[2, 4, 6]

Or with 3 lists

sum_lists([5,5,5,5,5], [10,10,10,10,10], [4,4,4,4,4])

Output:

[19, 19, 19, 19, 19]
Coffee and Code
  • 885
  • 14
  • 16
8

Perhaps "the most pythonic way" should include handling the case where list1 and list2 are not the same size. Applying some of these methods will quietly give you an answer. The numpy approach will let you know, most likely with a ValueError.

Example:

import numpy as np
>>> list1 = [ 1, 2 ]
>>> list2 = [ 1, 2, 3]
>>> list3 = [ 1 ]
>>> [a + b for a, b in zip(list1, list2)]
[2, 4]
>>> [a + b for a, b in zip(list1, list3)]
[2]
>>> a = np.array (list1)
>>> b = np.array (list2)
>>> a+b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (2) (3)

Which result might you want if this were in a function in your problem?

Fred Mitchell
  • 2,145
  • 2
  • 21
  • 29
  • in this case one should definitely look into `zip_longest` from itertools with a `fillvalue` of `0`. – Ma0 Jun 23 '20 at 07:41
7

This is simple with numpy.add()

import numpy

list1 = numpy.array([1, 2, 3])
list2 = numpy.array([4, 5, 6])
result = numpy.add(list1, list2) # result receive element-wise addition of list1 and list2
print(result)
array([5, 7, 9])

See doc here

If you want to receiver a python list:

result.tolist()
6

This will work for 2 or more lists; iterating through the list of lists, but using numpy addition to deal with elements of each list

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

lists = [list1, list2]
list_sum = np.zeros(len(list1))
for i in lists:
   list_sum += i
list_sum = list_sum.tolist()    

[5.0, 7.0, 9.0]
litepresence
  • 3,109
  • 1
  • 27
  • 35
6

If you need to handle lists of different sizes, worry not! The wonderful itertools module has you covered:

>>> from itertools import zip_longest
>>> list1 = [1,2,1]
>>> list2 = [2,1,2,3]
>>> [sum(x) for x in zip_longest(list1, list2, fillvalue=0)]
[3, 3, 3, 3]
>>>

In Python 2, zip_longest is called izip_longest.

See also this relevant answer and comment on another question.

Community
  • 1
  • 1
jjst
  • 2,631
  • 2
  • 22
  • 34
5
[list1[i] + list2[i] for i in range(len(list1))]
rayryeng
  • 102,964
  • 22
  • 184
  • 193
wgr
  • 513
  • 4
  • 9
4

Use map with lambda function:

>>> map(lambda x, y: x + y, list1, list2)
[5, 7, 9]
Peaters
  • 49
  • 3
3

I haven't timed it but I suspect this would be pretty quick:

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

list_sum = (np.add(list1, list2)).tolist()

[5, 7, 9]
litepresence
  • 3,109
  • 1
  • 27
  • 35
2

Although, the actual question does not want to iterate over the list to generate the result, but all the solutions that has been proposed does exactly that under-neath the hood!

To refresh: You cannot add two vectors without looking into all the vector elements. So, the algorithmic complexity of most of these solutions are Big-O(n). Where n is the dimension of the vector.

So, from an algorithmic point of view, using a for loop to iteratively generate the resulting list is logical and pythonic too. However, in addition, this method does not have the overhead of calling or importing any additional library.

# Assumption: The lists are of equal length.
resultList = [list1[i] + list2[i] for i in range(len(list1))]

The timings that are being showed/discussed here are system and implementation dependent, and cannot be reliable measure to measure the efficiency of the operation. In any case, the big O complexity of the vector addition operation is linear, meaning O(n).

Ehsan
  • 1,338
  • 14
  • 13
2
  • The zip function is useful here, used with a list comprehension v1, v2.
  • If you have a list of lists (instead of just two lists) you can use v3.
  • For lists with different length (for example: By adding 1 to the end of the first/secound list), then you can try something like this (using zip_longest) - v4
first = [1, 2, 3, 1]
second = [4, 5, 6]

output: [5, 7, 9, 1]
  • If you have an unknown number of lists of the same length, you can use the function v5.

  • v6 - The operator module exports a set of efficient functions corresponding to the intrinsic operators of Python. For example, operator.add(x, y) is equivalent to the expression x+y.

  • v7 - Assuming both lists first and second have same length, you do not need zip or anything else.

################
first = [1, 2, 3]
second = [4, 5, 6]

####### v1 ########
third1 = [sum(i) for i in zip(first,second)]

####### v2 ########
third2 = [x + y for x, y in zip(first, second)]

####### v3 ########
lists_of_lists = [[1, 2, 3], [4, 5, 6]]
third3 = [sum(x) for x in zip(*lists_of_lists)]

####### v4 ########
from itertools import zip_longest
third4 = list(map(sum, zip_longest(first, second, fillvalue=0)))

####### v5 ########
def sum_lists(*args):
    return list(map(sum, zip(*args)))

third5 = sum_lists(first, second)

####### v6 ########
import operator
third6 = list(map(operator.add, first,second))

####### v7 ########
third7 =[first[i]+second[i] for i in range(len(first))]

####### v(i) ########

print(third1) # [5, 7, 9]
print(third2) # [5, 7, 9]
print(third3) # [5, 7, 9]
print(third4) # [5, 7, 9]
print(third5) # [5, 7, 9]
print(third6) # [5, 7, 9]
print(third7) # [5, 7, 9]
Milovan Tomašević
  • 6,823
  • 1
  • 50
  • 42
  • From Review:  Code-only answers are discouraged on Stack Overflow because they don't explain how it solves the problem. Please edit your answer to explain what this code does and how it answers the question, so that it is useful to the OP as well as other users also with similar issues. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Thanks – sɐunıɔןɐqɐp Aug 05 '20 at 08:04
2

Several methods are

  1. Using Numpy
import numpy as np
x = np.array([2,3,3])
y = np.array([1,2,6])

print(type(x)) # <class 'numpy.ndarray'>
print(type(y)) # <class 'numpy.ndarray'>

print(x+y) # [3 5 9]
print(type(x+y)) # <class 'numpy.ndarray'>

In the above code, You can see input and output are NumPy array formats.

import numpy as np
list1=[4,2,2,5]
list2=[2,1,6,7]

print(type(list1)) # <class 'list'>
print(type(list2)) # <class 'list'>

print(np.add(list1,list2)) # [ 6  3  8 12]
print(type(np.add(list1,list2))) # <class 'numpy.ndarray'>

Here, Input and output are in different formats.

  1. Using Numpy add
import numpy as np
list1=[3, 1, 4]
list2=[0, 9, 7]

print(type(list1)) # <class 'list'>
print(type(list2)) # <class 'list'>

print(np.add(list1, list2).tolist()) # [3, 10, 11]
print(type(np.add(list1, list2).tolist())) # <class 'list'>

In this example, explicitly we are converting NumPy array to list type using to_list()

  1. Using Map and Lambda
list1=[1, 3, 3]
list2=[3, 6, 8]

print(map(lambda x,y:x+y, list1, list2)) # <map object at 0x7fea235260a0>
print(list(map(lambda x,y:x+y, list1, list2))) # [4, 9, 11]
  1. Using zip and list comprehension
list1=[3, 1, 3]
list2=[1, 1, 3]

print(type(list1)) # <class 'list'>
print(type(list2)) # <class 'list'>

print(x + y for x, y in zip(list1, list2)) # <generator object <genexpr> at 0x7f755307b6d0>
print(list(x + y for x, y in zip(list1, list2))) # [4, 2, 6]
print(type([x + y for x, y in zip(list1, list2)])) # <class 'list'>

print(sum(x) for x in zip(list1, list2)) # <generator object <genexpr> at 0x7f4c623e76d0>
print(list(sum(x) for x in zip(list1, list2))) # [4, 2, 6]
print(type([sum(x) for x in zip(list1, list2)])) # <class 'list'>
  1. Using Map and operator.add
from operator import add
list1=[3, 1, 3]
list2=[1, 1, 3]
print(list(map(add, list1, list2))) # [4, 2, 6]
Shubhank Gupta
  • 705
  • 2
  • 10
  • 27
1
a_list = []
b_list = []
for i in range(1,100):
    a_list.append(random.randint(1,100))

for i in range(1,100):
    a_list.append(random.randint(101,200))
[sum(x) for x in zip(a_list , b_list )]
DSBLR
  • 555
  • 5
  • 9