2

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!

1 Answer 1

6

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']]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.