list comprehension return a list and sum over in loop

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Dec 12 04:46:57 EST 2014


KK Sasa wrote:

> Hi there,
> 
> The list comprehension is results = [d2(t[k]) for k in xrange(1000)],
> where d2 is a function returning a list, say [x1,x2,x3,x4] for one
> example. So "results" is a list consisting of 1000 lists, each of length
> four. Here, what I want to get is the sum of 1000 lists, and then the
> result is a list of length four. Is there any efficient way to do this?
> Because I found it is slow in my case. I tried sum(d2(t[k]) for k in
> xrange(1000)), but it returned error: TypeError: unsupported operand
> type(s) for +: 'int' and 'list'. Thanks.

That's because sum() defaults to adding with a default value of 0:

py> sum([[1, 2], [3, 4]], 0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'list'
py> sum([[1, 2], [3, 4]], [])
[1, 2, 3, 4]


But don't do that! It will be slow.


I don't completely understand your requirements. You should show some
typical data, and the expected result. The business with the xrange and
t[k] and even d2() is probably irrelevant. The important part is summing
the lists. That could mean either of these two things:

results = [ [1, 2, 3, 4], 
            [5, 6, 7, 8],
            [9, 10, 11, 12]]

sum(results)
--> gives [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

The way to do this efficiently is:

results = []
for sublist in [d2(t[k]) for k in xrange(1000)]:
    results.extend(sublist)


Or perhaps you mean this:


results = [ [1, 2, 3, 4], 
            [5, 6, 7, 8],
            [9, 10, 11, 12]]

sum(results)
--> gives [15, 18, 21, 24]


I expect that the fastest, most efficient way to do this will be with the
third-party library numpy. But in pure Python, you can do this:

def add(alist, blist):
    if len(blist) != len(alist): raise ValueError
    for i, x in blist:
        alist[i] += x


results = [0]*4  # Like [0,0,0,0]
for sublist in [d2(t[k]) for k in xrange(1000)]:
    add(results, sublist)





-- 
Steven




More information about the Python-list mailing list