How to combine data from different list in their respective row?

Viewed 30

Take for example I have:

list1 = [0, 1, 2, 3]
list2 = [8, 7, 1, 7]
list3 = [1, 2, 3, 4]
combinedlist=[]

How do I ensure that the values will add up together in their respective rows such that the output for combinedlist will be:

[9, 10, 6, 14]
2 Answers
list1 = [0, 1, 2, 3]
list2 = [8, 7, 1, 7]
list3 = [1, 2, 3, 4]

out = [sum(v) for v in zip(list1, list2, list3)]
print(out)

Prints:

[9, 10, 6, 14]

You can try:

list1 = [0, 1, 2, 3]
list2 = [8, 7, 1, 7]
list3 = [1, 2, 3, 4]

combinedlist = [list1[i] + list2[i] + list3[i] for i in range(len(list1))]
print(combinedlist)

Output:

[9, 10, 6, 14]
Related