17 ms·
> sum((range(i) for i in range(5)), []) This won't work in Python 3 since `range` is a new type (and it's O(n²) anyway, so not missed). The second option is i
by Veedrac 11y ago
> sum((range(i) for i in range(5)), [])
This won't work in Python 3 since `range` is a new type (and it's O(n²) anyway, so not missed).
The second option is idiomatic IMHO.
- dalke 11y agoIndeed! I incorrectly thought I tested that under both versions. And I didn't notice the O() performance, which is: >>> for i in range(20): ... t1 = time.time() ... n = len(sum((list(range(i)) for i in range(2**i)), [])) ... t2 = time.time() ... print(i, t2-t1) ... 0 0.00010800361633300781 1 2.288818359375e-05 2 2.5033950805664062e-05 3 4.00543212890625e-05 4 7.295608520507812e-05 5 0.00017499923706054688 6 0.0005929470062255859 7 0.0024929046630859375 8 0.012469053268432617 9 0.14577507972717285 10 1.7278220653533936 11 17.82376503944397 ^C (MHO too, BTW.)