Is there an easier way to sum together items from a list than the code I've written below? I'm new to this and this seems somewhat unwieldy.
n = [3,5,7]
o = [4,10,8]
p = [4,10,5]
lists = [n, o, p]
def sumList(x):
return sum(x)
def listAdder(y):
count = 0
for item in y:
count += sumList(item)
return count
print listAdder(lists)
Something like:
from itertools import chain
n = [3,5,7]
o = [4,10,8]
p = [4,10,5]
print sum(chain(n, o, p))
# 56
This avoids creating an un-necessary list of items, since you pass them to chain
directly...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With