Couldn't you just do something like: def flatten(x): if isIterable(x): for y in x: yield from flatten(y) else: yield x Well, technically this is a generator, but it's easy enough to put its result in a list.
I found this on Stack Overflow a while back, and I've been using it in my Python code since then: [item for sublist in l for item in sublist]
If you do want to do this, the better method is:
itertools.chain.from_iterable()
That has the advantage of being lazy, as opposed to a list comprehension, and has the potential to be more optimised. It also doesn't rely on the somewhat obscure and relatively hard to read nested list comprehension syntax.Docs: https://docs.python.org/3.6/library/itertools.html#itertools...