how to make ["a","b",["c","d"],"e"] into ['a', 'b', 'c', 'd', 'e'] ?

Rustom Mody rustompmody at gmail.com
Thu Apr 10 01:38:25 EDT 2014


On Thursday, April 10, 2014 10:55:10 AM UTC+5:30, balaji marisetti wrote:
> There was long thread discussing flattening of a list on this list :).
> See the link below.

I dont think that thread is relevant to this question:
1. That (started with) a strange/cute way of using names
2. It does not work for heterogenous lists

# One level flatten version
# like the earlier thread without strange naming
# needed by the recursive one below
>>> def fl1(l): return [y for x in l for y in x]


# recursive flatten
>>> def fr(l):
...   if not isinstance(l,list): return [l]
...   return fl1([fr(x) for x in l])


You can replace the list-comps by generator-comps 




More information about the Python-list mailing list