
Robert D. answered 09/27/19
Software Engineer Specializing in Python
There are many solutions, the closest thing to a "one liner" would be this (I would recommend still calling a flatten function instead of using that convoluted one line of code all over your file though):
def flatten(element):
return sum( ([x] if not isinstance(x, list) else flatten(x) for x in element), [] )
here is the most trivial one in my opinion, using a combination of recursion and iteration:
def flatten(inputList):
finalList = []
for item in inputList:
if isinstance(item, list):
for deepItem in flatten(item):
finalList.append(deepItem)
else:
finalList.append(item)
return finalList
Hope this helps!