How to create a new list by adding elements from multiple lists of same length in python ?

Published: December 17, 2020

Tags: Python; List;

DMCA.com Protection Status

An example of how to sum elements from multiple lists of same size in python:

Lists of same length

Let's define for example 3 lists of same size in python:

a = [1,2,3,4]

b = [10,20,30,40]

c = [1,1,1,1]

Sum elements from multiple lists to create a new list

To create a new list by adding the element by adding elements from multiple lists, a solution is to use a list comprehension and zip:

d = [x+y+z for x, y, z in zip(a, b, c)]

print(d)

returns

[12, 23, 34, 45]

Sum all elements

Note: to add all elements together just use sum():

tot = sum(d)

print(tot)

returns

114