-3

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.

4 Answers4

3

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)]

OUTPUT:

[11, 13, 15, 17, 19]
Nk03
  • 14,699
  • 2
  • 8
  • 22
2

Use the vectorised operations from numpy

import numpy as np
newlist = list(np.array(list1) + np.array(list2))
braulio
  • 543
  • 2
  • 13
1
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]
1

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)