[Python-ideas] All ideas together: Fast sum() for non-numbers
Ronald Oussoren
ronaldoussoren at mac.com
Fri Jul 5 11:54:59 CEST 2013
On 5 Jul, 2013, at 11:28, Haoyi Li <haoyi.sg at gmail.com> wrote:
> Is there a better way to flatten lists than [item for sublist in l for item in sublist]? I naturally don't use sum() for much, but if i could, sum(my_list) looks much better than [item for sublist in my_list for item in sublist]. Looking at http://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python, all the alternatives seem equally obtuse and verbose. I have a lot of list flattenings I would love to use sum(my_list) for if I had the chance.
There's also list(itertools.chain.from_iterable(somelist)). I haven't benchmarked that, I don't need to flatten lists often enough that I need to worry about the performance.
Ronald
More information about the Python-ideas
mailing list