joining list elements

Roland Schlenker rol9999 at attglobal.net
Sat Oct 14 08:34:02 EDT 2000


Johannes Zellner wrote:

> how do I join list elements ?
> 
> e.g. I want [[1, 2, 3], [4, 5, 6]] --> [1, 2, 3, 4, 5, 6]
>      and    [[1, 2, 3],  4, 5, 6 ] --> [1, 2, 3, 4, 5, 6]
>      and    [1, 2, 3], 4 --> [1, 2, 3, 4]
              ^^^^^^^^^^^^
              I am not sure what you mean this.

This will work for the other two.

def flatten(aList):
    tmpList = []
    for item in aList:
        if type(item) == type([]):
            tmpList = tmpList + flatten(item)
        else:
            tmpList.append(item)
    return tmpList

Roland Schlenker



More information about the Python-list mailing list