flatten a list of list

Tim Cook timothywayne.cook at gmail.com
Mon Aug 17 13:30:44 EDT 2009


On Aug 16, 6:47 am, Terry <terry.yin... at gmail.com> wrote:
> Hi,
>
> Is there a simple way (the pythonic way) to flatten a list of list?
> rather than my current solution:
>
> new_list=[]
> for l in list_of_list:
>     new_list.extend(l)
>
> or,
>
> new_list=reduce(lambda x,y:x.extend(y), list_of_list)
>
> br, Terry

Well, This is not simple but it is comprhensive in that it has to do
several things.  I am using it to decompose deeply nested lists from
Pyparsing output that may have strings in a variety of languages.
Performance wise I do not know how it stacks up against the other
examples but it works for me.  :-)

def flatten(x):
    """flatten(sequence) -> list

    Returns a single, flat list which contains all elements retrieved
    from the sequence and all recursively contained sub-sequences
    (iterables). All strings are converted to unicode.

    """
    result = []
    for el in x:
        #if isinstance(el, (list, tuple)):
        if hasattr(el, "__iter__") and not isinstance(el, basestring):
            result.extend(flatten(el))
        else:
            result.append(el)


    # all strings must be unicode
    rtnlist=[]
    for x in result:
        if isinstance(x,str):
            # replace any brackets so Python doesn't think it's a list
and we still have a seperator.
            x=x.replace('[','_')
            x=x.replace(']','_')
            try:
                x=unicode(x, "utf8")  # need more decode types here
            except UnicodeDecodeError:
                x=unicode(x, "latin1")
            except UnicodeDecodeError:
                x=unicode(x,"iso-8859-1")
            except UnicodeDecodeError:
                x=unicode(x,"eucJP")

        rtnlist.append(x)

    return rtnlist



More information about the Python-list mailing list