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

P

pete.bee.emm

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.


x=["a","b",["c","d"],"e"]
y=x[2]
y ['c', 'd']
x.insert(2,y[0])
x

['a', 'b', 'c', ['c', 'd'], 'e']
['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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top