On Wed, Jun 16, 2021 at 8:04 PM David Mertz <me...@gnosis.cx> wrote:

>  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,
>

IIUC, the idea behind raising, rather than optimizing, is that there is a
"better" way to do it, and it's a good lesson. After all, sum() aside, we
also want to discourage folks from doing a lot of string concatenation in a
loop as well.

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.
>

I agree -- str.join() is built in and really idiomatic Python --
itertools.chain is, I suppose idiomatic Python, but there's the import, a
lot of extra typing, and a substantial cognitive load to figure it out the
first (few) times you use it. (I don't think I ever have...)

If, as suggested, flattening a list of lists is a common operation, a nice
clean and efficient built in way to do it would be reasonable. Heck, you
could make it a list method :-)

If you read the BPO the OP linked, that was a suggested patch to optimize
sum(list_of_lists) -- I'm not sure that's such a bad idea after all.

-CHB

NOTE: One reason I've never had to flatten a list of lists is because I'm a
heavy numpy user -- I'd be more likely to have an ndarray and flatten that
-- which is very easy and efficient :-)

 --
Christopher Barker, PhD (Chris)

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/T5UUKFQXARJ34YIERO7AFS4FBRP6QIEJ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to