[Python-ideas] All ideas together: Fast sum() for non-numbers
Oscar Benjamin
oscar.j.benjamin at gmail.com
Fri Jul 5 11:56:10 CEST 2013
On 5 July 2013 10: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.
from itertools import chain
def listjoin(iterable_of_iterables):
return list(chain.from_iterable(iterable_of_iterables))
Oscar
More information about the Python-ideas
mailing list