A multidimensional list like l=[[1,2],[3,4]] could be converted to a 1D one by doing sum(l,[]). Can anybody please explain how that happens?
l=[[1,2],[3,4]]
sum(l,[])
For any kind of multidiamentional array, this code will do flattening to one dimension :
def flatten(l): try: return flatten(l[0]) + (flatten(l[1:]) if len(l) > 1 else []) if type(l) is list else [l] except IndexError: return []