Flatten an irregular list of lists
Using generator functions can make your example a little easier to read and probably boost the performance.
Python 2
def flatten(l):
for el in l:
if isinstance(el, collections.Iterable) and not isinstance(el, basestring):
for sub in flatten(el):
yield sub
else:
yield el
I used the Iterable ABC added in 2.6.
Python 3
In Python 3, the basestring
is no more, but you can use a tuple of str
and bytes
to get the same effect there.
The yield from
operator returns an item from a generator one at a time. This syntax for delegating to a subgenerator was added in 3.3
from collections.abc import Iterable
def flatten(l):
for el in l:
if isinstance(el, Iterable) and not isinstance(el, (str, bytes)):
yield from flatten(el)
else:
yield el
My solution:
import collections
def flatten(x):
if isinstance(x, collections.Iterable):
return [a for i in x for a in flatten(i)]
else:
return [x]
A little more concise, but pretty much the same.
Generator using recursion and duck typing (updated for Python 3):
def flatten(L):
for item in L:
try:
yield from flatten(item)
except TypeError:
yield item
list(flatten([[[1, 2, 3], [4, 5]], 6]))
>>>[1, 2, 3, 4, 5, 6]