Explanation: There is list (can contain several nested lists, variables, int, str). For example:
b = 15
list = [1,2,3,4,[5,6,7,8,[9,["a",b,"c"],10,11]]]
I would like my input to be:
1, 2, 3 , 4 ,5 ,6, 7, 8, 9, a, b, c, 10, 11
Is there any function that can convert any list with any nested level to a string?
I know in reality it is not the case that will always happen, however we do not always know what is inside of the list to write specific function or iteration/loop.
P.S. I have tried several methods already closed/discussed (join for example) on StackOverflow, however those work only for simple or specific lists.
Thanks in advance.
Edited:
def flatten(lis):
"""Given a list, possibly nested to any level, return it flattened."""
new_lis = []
for item in lis:
if isinstance(item, list):
new_lis.extend(flatten(item))
else:
new_lis.append(item)
return new_lis