I have the following list
In [13]: nested_list=[0,25,[0,2,3,4],[1,1,-1,-1]]
and I'd like to flatten it as follows:
[0,25,0,2,3,4,1,1,-1,-1]
using the following list comprehension
[y for y in x if isinstance(x,list) else x for x in nested_list]
But I'm getting this error
In [16]: [y for y in x if isinstance(x,list) else x for x in nested_list]
File "<ipython-input-16-e49b6b9924a1>", line 1
[y for y in x if isinstance(x,list) else x for x in nested_list]
^
SyntaxError: invalid syntax
I know there are multiple solutions not using a list comprehension but recursion. However, I'd like to use a list comprehension. Can someone advice as to the correct syntax ?