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

Ben Finney ben+python at benfinney.id.au
Thu Apr 10 01:25:52 EDT 2014


length power <elearn2014 at gmail.com> writes:

> maybe there is a more smart way to do.

Maybe. But a way to do what, exactly?

You start with a list, but what is it exactly that you want to do with
that list?

    >>> x = ["a", "b", ["c", "d"], "e"]

If I interpret your request *literally*, I can achieve it in a single
statement::

    >>> y = ['a', 'b', 'c', 'd', 'e']

There! We got the second list you wanted.

Maybe you want to turn the second item in any list into ‘['c', 'd']’::

    >>> y = x[:2] + ['c', 'd'] + x[3:]

Or maybe you want some other operation. It's not clear from your example.

So, thank you for providing an example of what you mean; but that
doesn't help with knowing what you mean *generally*.

What is the general operation you want to do? Please describe what input
you will get, and what the output should be in the same terms.

-- 
 \        “Ubi dubium, ibi libertas.” (“Where there is doubt, there is |
  `\                                                        freedom.”) |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list