I've found solutions before, but I'm wondering what the fastest solution is to flatten lists which contain other lists of arbitrary length.
[1, 2, [3, 4, ,], ]
There can be infinitely many levels. Some of the list objects can be strings, which mustn't be flattened into their sequential characters in the output list.
Here's a recursive approach that is string friendly:
nests = [1, 2, [3, 4, ,['hi']], [6, [[[7, 'hello']]]]] def flatten(container): for i in container: if isinstance(i, (list,tuple)): for j in flatten(i): yield j else: yield i print list(flatten(nests))
[1, 2, 3, 4, 5, 'hi', 6, 7, 'hello']
Note, this doesn't make any guarantees for speed or overhead use, but illustrates a recursive solution that hopefully will be helpful.
It doesn't have to be recursive. In fact, an iterative solution is often faster because of the overhead involved in function calls. Here's an iterative version I wrote a while back:
def flatten(items, seqtypes=(list, tuple)): for i, x in enumerate(items): while i < len(items) and isinstance(items[i], seqtypes): items[i:i+1] = items[i] return items
Haven't tested the performance of this specific implementation, but it is probably not so great because of all the slice assignments, which could end up moving a lot of memory around. Still, don't assume it has to be recursive, or that it's simpler to write it that way.
This implementation does have the advantage of flattening the list "in place" rather than returning a copy, as recursive solutions invariably do. This could be useful when memory is tight. If you want a flattened copy, just pass in a shallow copy of the list you want to flatten:
flatten(mylist) # flattens existing list newlist = flatten(mylist[:]) # makes a flattened copy
Also, this algorithm is not limited by the Python recursion limit because it's not recursive. I'm certain this will virtually never come into play, however.