Flattening lists in Python

Can anyone do better than this ugly hack?

def flatten(x):
flat = True
ans = []
for i in x:
if ( i.__class__ is list):
ans = flatten(i)
return ans

Update. I like this solution proposed by one of the commenters (sweavo):

def flatten(l):
if isinstance(l,list):
return sum(map(flatten,l))
return l

Can anyone do better?

See also my posts YouTube scalability, Yield returns are not esoteric anymore, Efficient FIFO/Queue data structure in Python and Autocompletion in the Python console.

Subscribe to this blog
in a reader
or by Email.

Tweet about this on Twitter0Share on Facebook0Share on Google+0Share on Reddit0Email this to someone

16 thoughts on “Flattening lists in Python”

  1. I think you just want isinstance, but here’s a nice recursive function:

    How about:

    def flatten(x):
    if not isinstance(x,list):
    return x
    elif len(x) is 0:
    return []
    elif isinstance(x[0],list):
    return flatten(x[0]) + flatten(x[1:])
    return [x[0]] + flatten(x[1:])

  2. No, Daniel, I don’t know a better way ATM.
    A possible reason for this obvious lack could be that the Python community likes to have functions with clear semantics. For example, sometimes I want to flatten only one level, or different types, for example, given this input:

    [[[1,2,3], (42,None)], [4,5], [6], 7, MyVector(8,9,10)]

    One could imagine the following outputs:

    # flattened one level:
    [[1, 2, 3], (42, None), 4, 5, 6, 7, MyVector(8,9,10)]

    # flattened all lists:
    [1, 2, 3, (42, None), 4, 5, 6, 7, MyVector(8,9,10)]

    # flattened all lists and tuples:
    [1, 2, 3, 42, None, 4, 5, 6, 7, MyVector(8,9,10)]

    # flattened all iterables:
    [1, 2, 3, 42, None, 4, 5, 6, 7, 8, 9, 10]

    @Will: Daniels version looks more optimized to me. The question whether isinstance is better than checking the class is another possible implementation decision, e.g. given the following definition of MyVector:

    class MyVector(list):
    def __init__(self, *args):
    list.__init__(self, args)

    Your version would handle it the same as a list, while daniels would not flatten it.

    For the record, the above corresponds to the following checks in Daniel’s flatten function:
    if i.__class__ is list:
    if i.__class__ in (list, tuple):
    if isinstance(i, list):
    if isinstance(i, (list, tuple)):
    if hasattr(i, “__iter__”):
    AFAICS the simplest way to flatten only one layer is to change the recursive call to “list” instead of “flatten”.

  3. Here is a version from the mailing list:

    def flatten(seq):
    res = []
    for item in seq:
    if (isinstance(item, (tuple, list))):
    return res

    Another version is listed a in recipe 363051:

    import sys
    def flatten(inlist, ltype=(list,tuple), maxint=sys.maxint):
    for ind in xrange(maxint):
    while isinstance(inlist[ind], ltype):
    inlist[ind:ind+1] = list(inlist[ind])
    except IndexError:
    return inlist

  4. def flatten(x):
    result = []
    for v in x:
    if hasattr(v, ‘__iter__’) and not isinstance(v, basestring):
    return result

  5. As far as I can tell, several of the functions presented above do not work, including sweavo’s. Thie function below does work.

    def flatten(x):
    ans = []
    for i in range(len(x)):
    if isinstance(x[i],list):
    ans = x[:i]+flatten(x[i])+x[i+1:]
    return ans

  6. Sorry, my previous post didn’t work either. But this one does. Honest.

    def flatten(x):
    ans = []
    for i in range(len(x)):
    if isinstance(x[i],list):
    return ans

  7. A modified version of sweavo’s post (which didn’t work for me either):

    def flatten(l):
    if isinstance(l,list):
    return sum(map(flatten,l),[])
    return [l]

  8. What about using the reduce-function?


    def flatten(l):
    return reduce(operator.add, l)

    Obviously this requires the operator-module import.

  9. There is a problem with most of these in that they’re recursive. Why is it a problem? Because python’s stack is going to blow up when the depth of the list is something ridiculously large.

    So, we must do this with iteration. One way is to iterate over the list until no lists remain:

    def flatten(lst):
    has_lists = True
    while has_lists:
    tmp_lst = []
    has_lists = False
    for elt in lst:
    if isinstance(elt, list):
    tmp_lst += elt
    has_lists = True
    lst = tmp_lst
    return lst

    Of course, you’re doing more iteration that you need to if the list contains elements of variable list depth. But since function calls are expensive in Python, it may actually be faster than recursion.

  10. works with recursive arrays too… maybe a bit too long though

    def flatten(inlist):
    res = []
    def append(l):
    try: res.extend(flatten(l))
    except TypeError:res.append(l)
    map(append, inlist)
    return res

    >>> print flatten([[[[1]]],[2],[[[3],4,5],6],7])

  11. ahh spaces got cut.
    def flatten(inlist):
    ____ res = []
    ____ def append(l):
    ________ try: res.extend(flatten(l))
    ________ except TypeError:res.append(l)
    ____ map(append, inlist)
    ____ return res

  12. If you don’t mind losing the order of the list, you can do it with a nonrecursive oneliner;

    lambda X:(lambda x:filter(lambda x:not hasattr(x,’__iter__’),[[x.append(j) for j in i] if hasattr(i,’__iter__’) else i for i in x]))(list(X))

  13. >>> def flatten(l):
    … return reduce(lambda a,b: a + (flatten(b) if hasattr(b, ‘__iter__’) else [b]), l, [])

    >>> flatten([1,[2,3,[4,[5,6],7],8,[9,10,[11]]]])
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

Leave a Reply

Your email address will not be published. Required fields are marked *