disgrating a list

Neil Cerutti horpner at yahoo.com
Fri Sep 1 14:55:46 EDT 2006


On 2006-09-01, jwaixs <jwaixs at gmail.com> wrote:
> Hello,
>
> How can I disgrate (probably not a good word for it) a list? For
> example:
>
> a = [1,2]
> b = 3
> c = [a] + [b]     # which makes [[1,2],3]
>
> Then how can I change c ([[1,2],3]) into [1,2,3]? I have a
> simple function for this:

You might try:

  c = a + [b]

instead, to avoid the issue.

> But this function isn't really doing it in the "python-way".
> Doesn't have python an obscure function or buildin to do this?

I don't know if the following is the Python way, but I think it's
cute:

def flatten(x):
    """Flatten list x, in place."""    
    i = 0
    while i < len(x):
        if isinstance(x[i], list):
            x[i:i+1] = x[i]
        i = i+1

-- 
Neil Cerutti
In my prime I could have handled Michael Jordan.  Of course, he
would be only 12 years old. --Jerry Sloan



More information about the Python-list mailing list