flattening lists

Dan Stromberg drsalists at gmail.com
Tue Oct 11 22:46:21 EDT 2022


On Tue, Oct 11, 2022 at 12:48 PM SquidBits _ <giladsmum at gmail.com> wrote:

> Does anyone else think there should be a flatten () function, which just
> turns a multi-dimensional list into a one-dimensional list in the order
> it's in. e.g.
>
> [[1,2,3],[4,5,6,7],[8,9]] becomes [1,2,3,4,5,6,7,8,9].
>
> I have had to flatten lists quite a few times and it's quite tedious to
> type out. It feels like this should be something built in to python, anyone
> else think this way?
>

I think the usual argument against putting something like this in the
standard library (I hope it won't be built in), is that there are many ways
to define "flatten".  That is, should it only go one level deep?   All the
way to the bottom?  n levels deep?  Should it do something special with
lists, dicts, tuples, sets?

This looks like a nice URL on the topic:
https://www.pythonpool.com/flatten-list-python/


More information about the Python-list mailing list