f



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

--001a11c34e8edbc7c404f6a94bbe
Content-Type: text/plain; charset=ISO-8859-1

>>> 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.

--001a11c34e8edbc7c404f6a94bbe
Content-Type: text/html; charset=ISO-8859-1

<div dir="ltr"><div>&gt;&gt;&gt; x=[&quot;a&quot;,&quot;b&quot;,[&quot;c&quot;,&quot;d&quot;],&quot;e&quot;]<br>&gt;&gt;&gt; y=x[2]<br>&gt;&gt;&gt; y<br>[&#39;c&#39;, &#39;d&#39;]<br>&gt;&gt;&gt; x.insert(2,y[0])<br>&gt;&gt;&gt; x<br>
[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, [&#39;c&#39;, &#39;d&#39;], &#39;e&#39;]<br>&gt;&gt;&gt; x.insert(3,y[1])<br>&gt;&gt;&gt; x<br>[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, [&#39;c&#39;, &#39;d&#39;], &#39;e&#39;]<br>
&gt;&gt;&gt; del x[4]<br>&gt;&gt;&gt; x<br>[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;]<br>&gt;&gt;&gt;<br></div>maybe there is a more smart way to do.<br></div>

--001a11c34e8edbc7c404f6a94bbe--
0
length
4/10/2014 5:14:49 AM
comp.lang.python 77058 articles. 6 followers. Post Follow

1 Replies
1610 Views

Similar Articles

[PageSpeed] 23

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.
0
pete
4/10/2014 5:36:46 PM
Reply: