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

Andrew Barnert abarnert at yahoo.com
Tue Jul 9 03:02:49 CEST 2013


From: Haoyi Li <haoyi.sg at gmail.com>
Sent: Monday, July 8, 2013 5:39 PM


> I for one find it annoying that i have to write a verbose long thingy every time i need to flatten lists

What verbose long thingy? You just need to write:

    flatten = itertools.chain.from_iterable

Or, if you use more-itertools:

    from more_itertools import flatten

Someone suggested moving this to builtins as "chain" or "flatten" or similar. I'm at least +0 on this. 

At any rate, it's about as simple as can be. 

Because it gives you an iterable, you don't pay the cost (in time or space) of building a list unless you need to, which means in many use cases it's actually much faster than sum, at the cost of being a little bit slower for lists when you do need a list.


It also flattens any iterable of sequences, or even any iterable of iterables, in the same time—linear in the total size. With no custom optimizations, it works with tuples, blist.sortedlists, cons lists, or _anything else you can throw at it_.

And it's obvious what it does. If you sum three sortedlists, do you get the first list's elements, then the second, then the third, or a merge of the three? I don't know. If you chain or flatten three sortedlists, it's obvious which one you get.



More information about the Python-ideas mailing list