3 ms·
Other options for your first case are: >>> sum((range(i) for i in range(5)), []) [0, 0, 1, 0, 1, 2, 0, 1, 2, 3] and >>> import itertools >>>
by dalke 11y ago
Other options for your first case are:
>>> sum((range(i) for i in range(5)), [])
[0, 0, 1, 0, 1, 2, 0, 1, 2, 3]
and
>>> import itertools
>>> list(itertools.chain.from_iterable(range(i) for i in range(5)))
[0, 0, 1, 0, 1, 2, 0, 1, 2, 3]
"filter keeping the type". That was only true for some types. Python 2.7's filter does not maintain the set type:
>>> filter(lambda x: x in 'ABC', {'A','B','C','D','E','F','A'})
['A', 'C', 'B']
Regarding the cosmetic feature, that's a consequence of PEP 3113 -- Removal of Tuple Parameter Unpacking , https://www.python.org/dev/peps/pep-3113/ https://www.python.org/dev/peps/pep-3113/ , which also applies to functions.
I use 's.encode("hex")' often. I realize why hex/base64 are gone, but it's so short and easy in Python 2.7.
- 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.)
- deleted 11y ago[deleted]