disgrating a list

Hardcoded Software hardcoded.software at gmail.com
Fri Sep 1 14:19:34 EDT 2006


Tim Chase wrote:
>  >>> def flatten(x):
> ...     q = []
> ...     for v in x:
> ...             if hasattr(v, '__iter__'):
> ...                     q.extend(flatten(v))
> ...             else:
> ...                     q.append(v)
> ...     return q
> ...

Let's do some nitpicking on "pythonic" style. A more pythonic way to do
it would be:

try:
	q.extend(v)
except TypeError:
	q.append(v)




More information about the Python-list mailing list