[Python-ideas] All ideas together: Fast sum() for non-numbers

Paul Moore p.f.moore at gmail.com
Fri Jul 5 12:50:06 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.
>

Only partially facetious, but

newlist = flatten(list_of_lists)

In other words, if you don't like the nested comprehension, hide it in a
personal function. No need to force the functionality into an existing
builtin,

Not-every-one-liner-needs-to-be-a-builtin-ly y'rs,
Paul
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130705/7c7b2d27/attachment.html>


More information about the Python-ideas mailing list