flattening lists
Thomas Passin
list1 at tompassin.net
Tue Oct 11 16:27:07 EDT 2022
Is this what you usually do?
l1 = [[1,2,3],[4,5,6,7],[8,9]]
l2 = []
for lz in l1:
l2.extend(lz)
print(l2) # [1,2,3,4,5,6,7,8,9]
Not all that "tedious", perhaps... I tend to accumulate little utilities
like this in a file and point to it with a .pth file. Of course, you
have to remember to include it if you share your program with someone else.
On 10/11/2022 3:32 PM, SquidBits _ 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?
More information about the Python-list
mailing list