Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

list

row

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]
like image 786
Xavier Avatar asked Nov 30 '25 08:11

Xavier


1 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]
like image 93
Andrej Kesely Avatar answered Dec 01 '25 20:12

Andrej Kesely