flattening lists

Antoon Pardon antoon.pardon at vub.be
Wed Oct 12 12:45:39 EDT 2022



Op 11/10/2022 om 21:32 schreef SquidBits _:
> 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?

Depending on what you exactly mean by "flatten", it already is easy to 
flatten a list in python:

 >>> lst = [[1,2,3],[4,5,6,7],[8,9]]
 >>> list(itertools.chain.from_iterable(lst))
[1, 2, 3, 4, 5, 6, 7, 8, 9]

-- 
Antoon Pardon


More information about the Python-list mailing list