Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I've seen it used for a long time. The only problem with it is the quadratic time complexity: it's equivalent to ((N0 + N1) + N2) + N3 ... so you copy the same elements over and over again as the sum builtin doesn't know to use anything but the + operator to build each element. (I vaguely recall some discussion about making it more efficient on sequences of sequences).

A more efficient version would be: def sumseq(l): r = []; map(r.extend,l); return r

which would also work on any type of sequence as input, even if mixed. A quick test shows the quadratic time complexity being noticeable at about 128 items, where sumseq is 10x as fast and 1751x as fast at 16384 items.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: