1

I have an issue I cannot go thorugh. I need list of lists instead of list of lists of lists? Is there any simple command that 'unlists' one level of such 'deep' list in Python? Here is what I have:

[[['this', 'is', 'the', 'good', 'machine', 'learning', 'book']],
[['this', 'is', 'another', 'book']]]

and where I need to get to:

[['this', 'is', 'the', 'good', 'machine', 'learning', 'book'],
['this', 'is',  'another', 'book']]

I was trying to resolve it with function below

from pandas.core.common import flatten
list(flatten(x))

...but it breaks everything into one list...

['this', 'is', 'the', 'good', 'machine', 'learning', 'book', 'this', 'is',  'another', 'book']

Pls help :)

2 Answers2

2

You can simply apply it only to the sublists:

[list(flatten(y)) for y in x]
user2390182
  • 72,016
  • 6
  • 67
  • 89
1
[item for sub_list in your_list for item in sub_list]