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

pete.bee.emm at gmail.com pete.bee.emm at gmail.com
Thu Apr 10 13:36:46 EDT 2014


I've been using compiler.ast.flatten, but I have comments indicating it will need be replaced if/when I move to Python 3. 

I don't pollute my code base with flatten, I just call my own version in my utility library that is currently redirecting to flatten.

flatten works equally well with tuples as lists and I'm going to remain consistent with that. My version returns a tuple as well. 

My love affair with the immutable, hashable, and wonderfully named tuple is perhaps something that needs its own thread, or private room. 


On Wednesday, April 9, 2014 10:14:49 PM UTC-7, length power wrote:
> >>> x=["a","b",["c","d"],"e"]
> >>> y=x[2]
> >>> y
> ['c', 'd']
> >>> x.insert(2,y[0])
> >>> x
> 
> ['a', 'b', 'c', ['c', 'd'], 'e']
> >>> x.insert(3,y[1])
> >>> x
> ['a', 'b', 'c', 'd', ['c', 'd'], 'e']
> 
> >>> del x[4]
> >>> x
> ['a', 'b', 'c', 'd', 'e']
> >>>
> maybe there is a more smart way to do.



More information about the Python-list mailing list