Possible Duplicate:
Flatten (an irregular) list of lists in Python
I have the following list —
[1,[2,3],4,[5,[6,7]]]
And I need to make it flat —
[1,2,3,4,5,6,7]
To do this, I am currently using a for loop with isinstance, with the number of loops being #nests - 1.
What woud be the simplest way to make the nested list flat? Thank you.
A similar question which deals with making a flat list out of nested lists (only) can be found here: Making a flat list out of list of lists in Python.
Hauled from webhelpers.misc.flatten
EDIT: The iterable test here is quite naive and can be improved by checking for the presence of
__iter__or an instance of thecollections.Iterableabstract base class.EDIT 2: @ChristopheD is absolutely correct, this is a dupe so head over to the linked question.