I have 2 lists:
list1 = [1,2,3,4,5]
list2 = [10,11,12,13,14]
And I want to sum, each the elements of the lists, like list1[0]+list2[0], list1[1]+list2[1]....And have a new list:
newlist = [11,13,15,17,19]
Thanks.
I have 2 lists:
list1 = [1,2,3,4,5]
list2 = [10,11,12,13,14]
And I want to sum, each the elements of the lists, like list1[0]+list2[0], list1[1]+list2[1]....And have a new list:
newlist = [11,13,15,17,19]
Thanks.
you can use zip
/map
:
result = list(map(sum,zip(list1,list2)))
Alternative, via list_comprehension
:
result = [i+j for i,j in zip(list1,list2)]
[11, 13, 15, 17, 19]
Use the vectorised operations from numpy
import numpy as np
newlist = list(np.array(list1) + np.array(list2))
list1 = [1, 2, 3, 4, 5]
list2 = [10, 11, 12, 13, 14]
zipped_lists = zip(list1, list2)
sum = [x + y for (x, y) in zipped_lists]
Simple for loop
list1 = [1,2,3,4,5]
list2 = [10,11,12,13,14]
result = [list1[i]+list2[i] for i in range(len(list1))]
print(result)