On Sat, Dec 13, 2008 at 11:58 AM, Guilherme Polo email@example.com wrote:
Probably many of you have seen/written/used a lot of recipes for flattening lists/tuples, several of them are similar diverging just a bit, some uses more obscure code than others, some are not fast enough, but I have noticed all those have one thing in common: none of them mention _tkinter._flatten, not even in comments (if the site allows that).
Apparently _tkinter._flatten is unknown, and it being marked private doesn't help, and it also lives under _tkinter but doesn't depend on anything from tcl/tk. This _flatten has the advantage of being faster then the alternatives I have seen coded in Python, since it is done in C and its code is simple and it doesn't try to be too smart. It is also already part of Python, it is just unknown to most apparently.
So, I would like to know what do you think about moving _tkinter._flatten to some other module and renaming it to "flatten" ?
The problem is people often have a datastructure like this:
x = [['foo'], ['bar', 'baz'], ['quux']]
Although _flatten seems to work, it's actually overkill. It treats the input as a tree (using type checks to differentiate leaf from branch), rather than as a mere nested list.
The simplest solution is itertools.chain(*x).
If we did want to generalize flatten it should be made into iterator form and take an arbitrary predicate function to distinguish leaf vs branch. The default should either be depth based (subsuming chain, probably good in the long run) or use an appropriate ABC. I prefer depth based.
-- Adam Olsen, aka Rhamphoryncus