Alex Nikitin
Alex Nikitin

Reputation: 524

Split string inside nested list

I have a list like this:

 lst = [['one two', 'three'], ['four five', 'six']]

I need to make:

lst = [['one', 'two', 'three'], ['four', 'five', 'six']]

Tried ([i[0].split() for i in lst]) but it gives only [['one', 'two'], ['four', 'five']] Any ideas how to manage it? Thanks in advance!

Upvotes: 2

Views: 122

Answers (1)

cs95
cs95

Reputation: 402483

Maybe I'm oversimplifying this, but you can just join and re-split?

>>> [' '.join(x).split() for x in lst]
[['one', 'two', 'three'], ['four', 'five', 'six']]

Or, using the equivalent map method:

>>> list(map(str.split, map(' '.join, lst)))
[['one', 'two', 'three'], ['four', 'five', 'six']]

Upvotes: 6

Related Questions