Flatten a nested array/tuple
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | flatten = lambda arr: reduce(lambda x, y: ((isinstance(y, (list, tuple)) or x.append(y)) and x.extend(flatten(y))) or x, arr, [])
def flatten(array):
"""
Returns a list o flatten elements of every inner lists (or tuples)
****RECURSIVE****
"""
res = []
for el in array:
if isinstance(el, (list, tuple)):
res.extend(flatten(el))
continue
res.append(el)
return res
>>> a = [0, 10, 20, [30, (40, 50, [60, [70, [80]], {'hello': 'world'}]), 90], set(['world', 'hello'])]
>>> flatten(a)
[0, 10, 20, 30, 40, 50, 60, 70, 80, {'hello': 'world'}, 90, set(['world', 'hello'])]
|
Recently i needed to flat a list of elements (don't ask why or how, i just needed) and all i could find on internet was some flatten for 2-levels depth arrays, so i wrote my own function and posted here if can be of any use for anyone.
the 2 versions, lambda and standard "def ...", do the same work. I just did them both for the sake of someone although i prefer the second one (i could use comments and it's a little faster)
same but non recursive version:
I tried several flatten routines (see below) and find qflatten to be fastest (at least on my pc with python2.6); iflatten is like the iterative version above and p/rflatten are like the recursive original (with hopefully some speedups). The qflatten version is based on one that is in reportlab (also with some speed ups). Of course the speedups won't really work for very short inputs.
whoops the _qflatten is actually written
thanks for the replies. the ones i posted were something between readable and performance, few rows and instructions, that's why i choose the recursive way, even if i know that the make_flat logic is better for performance when you have to compute many operations.
the rflatten and iflatten makes a useful improvment to performance if you have a lot of inputs. and i'm sure that more improvements can be made, like
def flatten(*array):
andflatten(*el)
so you can use flatten(array1, array2, array3), if you want, instead of flatten([array1, array2, array3]).
ps: the lambda one was a joke :) ignore it: is not intended to use but some people wants the absolutely less rows for code, and go mad if they see a lambda solution, despite performance or readability