0

Im trying to write a function that get a list, and return a flatten one, for example

a=[1,2,[3,5,(2,3)]]
print(flat(a))
[1,2,3,5,(2,3)]

I wrote this :

def flat(lst,res=[]):
    if len(lst)==0:
        return res
    else:
        for element in lst:
            if isinstance(element,list):
                res.append(flat(element))
            res.append(element)
    return res

but my output is :

[1, 2, 3, 5, (2, 3), [...], [3, 5, (2, 3)]]

can you help me spot the mistake ?

limitless
  • 669
  • 7
  • 18
  • 3
    Possible duplicate of [Flatten (an irregular) list of lists in Python](http://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists-in-python) – 101 Jan 30 '16 at 13:53
  • ohh thank you i didnt saw this one. ill delete it. – limitless Jan 30 '16 at 13:54

0 Answers0