On Wed, Jun 16, 2021 at 7:35 PM Oliver Margetts <oliver.margetts@gmail.com> wrote:
I came across this just processing JSON data - flattening a list of 'deals' into a list of individual 'trades'. Flattening nested lists is not uncommon. And yes sum seemed the natural way to do it at the time.

I remember the discussion in 2013 about the quadratic performance of summing strings.  There were proposals for optimization of the string case, but they amounted to using the "fast path" sometimes present in `.__iadd__()` take over the `.__add__()` operation.  In fact, I gave keynotes at PyCon-UK 2013, and PyCon-ZA 2014 on exactly this discussion (but really more about what it shows about Python design: https://gnosis.cx/pycon-za-2014/Keynote-Ideas.pdf).

Even though I used the `sum(list_of_lists)` example in my presentations, I don't think I have EVER encountered it in the wild.

I'm sympathetic to raising an exception on `sum(list_of_lists)` similar to `sum(list_of_strings)`.  But what exactly is the recommended substitute?  We have this:

list(chain.from_iterable(list_of_lists))

Which is pretty nice, and what I'd probably do, but it DOES require importing from itertools to use it.  That feels like a drawback, at least a minor one.

--
The dead increasingly dominate and strangle both the living and the
not-yet born.  Vampiric capital and undead corporate persons abuse
the lives and control the thoughts of homo faber. Ideas, once born,
become abortifacients against new conceptions.