Skip to content
Advertisement

sum multiple list elements at the same time(python)

How can I sum multiple list elements at the same time? For example, something like this in Python:

Our lists (input):

[3, 3, 1, 1, 1, 1, 1]
[1, 1, 4, 5, 6, 7, 8]

Output:

[4, 4, 5, 6, 7, 8, 9]

Note: we don’t know how many list will be given to us.

Advertisement

Answer

This should do the job

l1 = [3, 3, 1, 1, 1, 1, 1]
l2 = [1, 1, 4, 5, 6, 7, 8]

l3 = [sum(t) for t in zip(l1, l2)]
    
print(l3)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement