I want to sum all second members of a nested list's sub-lists
Using sum
and a simple generator comprehension will do.
>>> lst = [[1,2], [3,4]]
>>> sum(sublist[1] for sublist in lst)
6
To make it more understandable / add some explanation:
>>> [sublist[1] for sublist in lst]
[2, 4]
creates a list of all second elements for every sublist in your list.
We pass that to the sum
function (with the minor difference that we are actually passing a generator to save memory, but sum([sublist[1] for sublist in lst])
would work just fine as well).