Easy way to add multiple list items?

Is there an easier way to summarize items from a list than the code I wrote below? I am new to this and it seems a little cumbersome.

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) 
+4
source share
4 answers

Sort of:

 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 unnecessary list of elements as you pass them directly to chain ...

+10
source

You can do this on one line, listAdder() same as calling sum() :

 sum(sum(x) for x in lists) 
+6
source

You can also exclude the list of lists and simply summarize directly:

sum(n+o+p)

+2
source

Be that as it may, it is not very effective, although

 sum(sum(lists, [])) 
+1
source

All Articles