Flattening lists

J Kenneth King james at agentultra.com
Thu Feb 5 11:45:55 EST 2009


mk <mrkafk at gmail.com> writes:

> Hello everybody,
>
> Any better solution than this?
>
> def flatten(x):
>     res = []
>     for el in x:
>         if isinstance(el,list):
>             res.extend(flatten(el))
>         else:
>             res.append(el)
>     return res
>
> a = [1, 2, 3, [4, 5, 6], [[7, 8], [9, 10]]]
> print flatten(a)
>
>
> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>
> Regards,
> mk

http://mail.python.org/pipermail/python-list/2005-July/330367.html



More information about the Python-list mailing list