For flatten, use:
flattened = sum(list_of_lists, ()) flattened = sum(list_of_lists, ()) ; CHICKEN Scheme
#;1> (flatten '((1 2 3) ((4 5) 6) (7 (8) (((((9))))))))
(1 2 3 4 5 6 7 8 9)
#;2> (apply append '((1 2 3) ((4 5) 6) (7 (8) (((((9))))))))
(1 2 3 (4 5) 6 7 (8) (((((9))))))
vs. # Python 3
>>> sum([[1,2,3], [[4,5],6], [7, [8], [[[[[9]]]]]]], [])
[1, 2, 3, [4, 5], 6, 7, [8], [[[[[9]]]]]]
There's a big difference here. Flattening a list to just the elements inside isn't terribly hard, especially in a language like Scheme with tail-recursion, but flatten is definitely something that should be in the standard library. The "flatten" you propose is really just appending the elements of the first level of the list.You are making the flattened list by continually concatenating the smaller lists. Each concatenation creates the new bigger list from scratch; the flattened list does not grow dynamically. This is quadratic-performance bad.
Use `list(itertools.chain.from_iterable(...))` instead.