4 ms·
Has the performance of sum on lists of lists in Python been fixed? It used to be pretty abysmal. But I suppose some would say that if you need to consider perfo
by Pinus 19d ago
Has the performance of sum on lists of lists in Python been fixed? It used to be pretty abysmal. But I suppose some would say that if you need to consider performance at all, you’re in the wrong language… :)
- theamk 18d agowow, TIL! Python 3.13.5 (main, Jul 15 2026, 20:25:40) [GCC 14.2.0] on linux >>> x = [[n]*1000 for n in range(1000)]; import timeit, itertools, functools, operator >>> timeit.timeit("len(list(sum(x, [])))", number=10, globals=globals()) 13.009033881127834 >>> timeit.timeit("len(list(list(functools.reduce(operator.add, x, []))))", number=10, globals=globals()) 12.941937348805368 >>> timeit.timeit("len(list(itertools.chain.from_iterable(x)))", number=10, globals=globals()) 0.0706032607704401 >>> timeit.timeit("out=[]; [out.extend(i) for i in x]; len(out)", number=10, globals=globals()) 0.06334403157234192 >>> timeit.timeit("len([i for a in x for i in a])", number=10, globals=globals()) 0.1232151910662651 mutable is fastest, itertools is just a bit slower, list comprehension is 2x slower, both "sum(..., [])" and "reduce" are 200 times slower!
- mahboi 18d agoYeah this is the kind of reason people dislike reduce