I think it worth noting that:
sum([[1],[2]], [])
Will also work, and I'm pretty sure will be faster then passing a lambda to reduce.
I was curious as to the speed of different methods, so I did some testing:
reduce(lambda a,b:a+b, x, []) 3644.38161492
reduce(list.__add__, x, []) 3609.44079709
sum(x,[]) 3526.84987307
y = [];for z in x: y.extend(z) 143.370306969
y = [];map(y.extend,x) 71.7020270824
y = [None]*400;del y[:];map(y.extend,x) 66.2245891094
list(itertools.chain(*x)) 102.285979986
list(itertools.chain.from_iterable(x)) 96.6231369972
[a for b in x for a in b] 203.764872074
And on PyPy (Because, why not)
reduce(lambda a,b:a+b, x, []) 4797.5895648
reduce(list.__add__, x, []) 4794.01214004
sum(x,[]) 4748.02929902
y = [];for z in x: y.extend(z) 56.9253079891
y = [];map(y.extend,x) 73.8642170429
y = [None]*400;del y[:];map(y.extend,x) 152.157783031
list(itertools.chain(*x)) 633.854824066
list(itertools.chain.from_iterable(x)) 629.917827129
[a for b in x for a in b] 89.6922459602
x = [[1,2,3,4],[2,3,4,5],[3,4,5,6],[4,5,6,7],[5,6,7,8],[6,7,8,9],[7,8,9,10],[8,9,10,11]]*100
Conclusions:
- Using a lambda in your reduce is slow
- The specialized
sum
function is faster then reduce
- Adding lists is slow.
- Python loop overhead is significant.