[Python-Dev] Suggestion for a new built-in - flatten

Michael Foord fuzzyman at voidspace.org.uk
Fri Sep 22 19:43:42 CEST 2006

Hello all,

I have a suggestion for a new Python built in function: 'flatten'.

This would (as if it needs explanation) take a single sequence, where 
each element can be a sequence (or iterable ?) nested to an arbitrary 
depth. It would return a flattened list. A useful restriction could be 
that it wouldn't expand strings :-)

I've needed this several times, and recently twice at work. There are 
several implementations in the Python cookbook. When I posted on my blog 
recently asking for one liners to flatten a list of lists (only 1 level 
of nesting), I had 26 responses, several of them saying it was a problem 
they had encountered before.

There are also numerous  places on the web bewailing the lack of this as 
a built-in. All of this points to the fact that it is something that 
would be appreciated as a built in.

There is an implementation already in Tkinter :

    import _tkinter._flatten as flatten

There are several different possible approaches in pure Python, but is 
this an idea that has legs ?

All the best,

Michael Foord

No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.405 / Virus Database: 268.12.7/454 - Release Date: 21/09/2006

More information about the Python-Dev mailing list