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]
answered Dec 27, 2012 at 7:12
8
From docs
import operator
list[map[operator.add, first,second]]
answered Dec 27, 2012 at 7:29
Thai TranThai Tran
9,6657 gold badges41 silver badges62 bronze badges
1
Default behavior in numpy.add
[numpy.subtract
, etc] is element-wise:
import numpy as np
np.add[first, second]
which outputs
array[[7,9,11,13,15]]
mirekphd
3,26025 silver badges44 bronze badges
answered Apr 28, 2014 at 19:57
user3582790user3582790
5894 silver badges2 bronze badges
7
Assuming both lists a
and b
have same length, you do not need zip, numpy or anything else.
Python 2.x and 3.x:
[a[i]+b[i] for i in range[len[a]]]
maro
4533 gold badges11 silver badges29 bronze badges
answered Jun 30, 2014 at 15:31
mathmath
8,24410 gold badges51 silver badges60 bronze badges
1
Try the following code:
first = [1, 2, 3, 4]
second = [2, 3, 4, 5]
third = map[sum, zip[first, second]]
kenorb
144k76 gold badges655 silver badges710 bronze badges
answered May 16, 2015 at 12:45
1
This extends itself to any number of lists:
[sum[sublist] for sublist in itertools.izip[*myListOfLists]]
In your case, myListOfLists
would be [first, second]
answered Dec 27, 2012 at 7:20
inspectorG4dgetinspectorG4dget
106k25 gold badges137 silver badges235 bronze badges
2
The easy way and fast way to do this is:
three = [sum[i] for i in zip[first,second]] # [7,9,11,13,15]
Alternatively, you can use numpy sum:
from numpy import sum
three = sum[[first,second], axis=0] # array[[7,9,11,13,15]]
answered Mar 17, 2013 at 9:25
ThiruThiru
3,0837 gold badges35 silver badges49 bronze badges
1
one-liner solution
list[map[lambda x,y: x+y, a,b]]
answered Jul 25, 2019 at 7:36
ShadowmanShadowman
611 silver badge4 bronze badges
first = [1, 2, 3, 4, 5]
second = [6, 7, 8, 9, 10]
three = list[map[sum, first, second]]
print[three]
# Output
[7, 9, 11, 13, 15]
KetZoomer
2,4863 gold badges14 silver badges37 bronze badges
answered Jul 21, 2017 at 8:11
Anurag MisraAnurag Misra
1,42217 silver badges23 bronze badges
1
If you have an unknown number of lists 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 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]
answered May 19, 2019 at 13:44
0
My answer is repeated with Thiru's that answered it in Mar 17 at 9:25.
It was simpler and quicker, here are his solutions:
The easy way and fast way to do this is:
three = [sum[i] for i in zip[first,second]] # [7,9,11,13,15]
Alternatively, you can use numpy sum:
from numpy import sum three = sum[[first,second], axis=0] # array[[7,9,11,13,15]]
You need numpy!
numpy array could do some operation like vectors
import numpy as np
a = [1,2,3,4,5]
b = [6,7,8,9,10]
c = list[np.array[a] + np.array[b]]
print c
# [7, 9, 11, 13, 15]
answered Oct 22, 2013 at 9:58
PiecePiece
212 bronze badges
1
What if you have list with different length, then you can try something like this [using zip_longest
]
from itertools import zip_longest # izip_longest for python2.x
l1 = [1, 2, 3]
l2 = [4, 5, 6, 7]
>>> list[map[sum, zip_longest[l1, l2, fillvalue=0]]]
[5, 7, 9, 7]
answered Apr 26, 2020 at 19:21
mohammed wazeemmohammed wazeem
1,2401 gold badge8 silver badges25 bronze badges
You can use zip[]
, which will "interleave" the two arrays together, and then map[]
, which will apply a function
to each element in an iterable:
>>> a = [1,2,3,4,5]
>>> b = [6,7,8,9,10]
>>> zip[a, b]
[[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]]
>>> map[lambda x: x[0] + x[1], zip[a, b]]
[7, 9, 11, 13, 15]
answered Dec 27, 2012 at 7:12
cdhowiecdhowie
149k23 gold badges278 silver badges290 bronze badges
Here is another way to do it. We make use of the internal __add__ function of python:
class SumList[object]:
def __init__[self, this_list]:
self.mylist = this_list
def __add__[self, other]:
new_list = []
zipped_list = zip[self.mylist, other.mylist]
for item in zipped_list:
new_list.append[item[0] + item[1]]
return SumList[new_list]
def __repr__[self]:
return str[self.mylist]
list1 = SumList[[1,2,3,4,5]]
list2 = SumList[[10,20,30,40,50]]
sum_list1_list2 = list1 + list2
print[sum_list1_list2]
Output
[11, 22, 33, 44, 55]
TrakJohnson
1,6292 gold badges17 silver badges30 bronze badges
answered Sep 23, 2016 at 19:18
StrykerStryker
5,34254 silver badges65 bronze badges
If you want to add also the rest of the values in the lists you can use this [this is working in Python3.5]
def addVectors[v1, v2]:
sum = [x + y for x, y in zip[v1, v2]]
if not len[v1] >= len[v2]:
sum += v2[len[v1]:]
else:
sum += v1[len[v2]:]
return sum
#for testing
if __name__=='__main__':
a = [1, 2]
b = [1, 2, 3, 4]
print[a]
print[b]
print[addVectors[a,b]]
answered May 10, 2017 at 10:59
first = [1,2,3,4,5]
second = [6,7,8,9,10]
#one way
third = [x + y for x, y in zip[first, second]]
print["third" , third]
#otherway
fourth = []
for i,j in zip[first,second]:
global fourth
fourth.append[i + j]
print["fourth" , fourth ]
#third [7, 9, 11, 13, 15]
#fourth [7, 9, 11, 13, 15]
answered Jul 5, 2017 at 11:49
Here is another way to do it.It is working fine for me .
N=int[input[]]
num1 = list[map[int, input[].split[]]]
num2 = list[map[int, input[].split[]]]
sum=[]
for i in range[0,N]:
sum.append[num1[i]+num2[i]]
for element in sum:
print[element, end=" "]
print[""]
Agney
17.1k7 gold badges50 silver badges73 bronze badges
answered Sep 11, 2017 at 19:49
j = min[len[l1], len[l2]]
l3 = [l1[i]+l2[i] for i in range[j]]
Cris Luengo
51.4k9 gold badges59 silver badges113 bronze badges
answered Dec 27, 2017 at 5:06
1
If you consider your lists as numpy array, then you need to easily sum them:
import numpy as np
third = np.array[first] + np.array[second]
print third
[7, 9, 11, 13, 15]
answered Apr 25, 2019 at 10:32
RadvinRadvin
1434 silver badges10 bronze badges
Perhaps the simplest approach:
first = [1,2,3,4,5]
second = [6,7,8,9,10]
three=[]
for i in range[0,5]:
three.append[first[i]+second[i]]
print[three]
answered Jul 26, 2018 at 15:20
first = [1,2,3,4,5]
second = [6,7,8,9,10]
third=[]
for i,j in zip[first,second]:
t=i+j
third.append[t]
print["Third List=",third]
output -- Third List= [7, 9, 11, 13, 15]
answered Sep 13 at 7:51
2
You can use this method but it will work only if both the list are of the same size:
first = [1, 2, 3, 4, 5]
second = [6, 7, 8, 9, 10]
third = []
a = len[first]
b = int[0]
while True:
x = first[b]
y = second[b]
ans = x + y
third.append[ans]
b = b + 1
if b == a:
break
print third
answered Dec 27, 2012 at 11:52
HelloUniHelloUni
4482 gold badges5 silver badges10 bronze badges