Skip to main content
1 of 3
user avatar
user avatar

It does come with such a method but it doesn't call it flatten. It's called "chain". It returns an iterator which you'd then need to use the list() function on to turn it back into a list. If you don't want to use a *, you can use the second "from_iterator" version. It works the same in Python 3. It will fail if the list input is not a list of lists.

[[1], [2, 3], [3, 4, 5]] #yes
[1, 2, [5, 6]] #no

As to why that method is not called flatten, that's hard to say. I suspect it might have something to do with van Rossum's discomfort with functional programming in certain sense.

user28988