I want to flatten a list of lists and items. The below code works only for a list of lists
def flatten(li):
return [item for sublist in li for item in sublist]
It would work on
[[1],[2,3],[4]]
but not
[[1],[2,3],[4],5]
I want a function that would do it for the above list - something like
[item if isinstance(sublist, list) else sublist for sublist in li
for item in sublist]
The above code gives an error.
Trying it on
[[1],[2,3],[4],5]
gives me
TypeError: 'int' object is not iterable
Can anyone give me a list comprehension which doesn't?
EDIT:
I want a LIST COMPREHENSION only.