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
- Previous message (by thread): how to make ["a", "b", ["c", "d"], "e"] into ['a', 'b', 'c', 'd', 'e'] ?
- Next message (by thread): how to make ["a","b",["c","d"],"e"] into ['a', 'b', 'c', 'd', 'e'] ?
- Messages sorted by:
[ date ]
[ thread ]
[ subject ]
[ author ]
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
- Previous message (by thread): how to make ["a", "b", ["c", "d"], "e"] into ['a', 'b', 'c', 'd', 'e'] ?
- Next message (by thread): how to make ["a","b",["c","d"],"e"] into ['a', 'b', 'c', 'd', 'e'] ?
- Messages sorted by:
[ date ]
[ thread ]
[ subject ]
[ author ]
More information about the Python-list
mailing list