1

I have a list like:

['Mark', 'Reynold', 'Peter', 'Randall Macenroe'] #The list is a lot longer, so I can't go by index

And I want to change that list into another list:

['Mark', 'Reynold', 'Peter', 'Randall', 'Macenroe']

How can I do that? I sure can use that space between the two names (there will be always a space between two names), but how?

1 Answer 1

5

You can use a list comprehension and str.split:

>>> lst = ['Mark', 'Reynold', 'Peter', 'Randall Macenroe']
>>> [y for x in lst for y in x.split()]
['Mark', 'Reynold', 'Peter', 'Randall', 'Macenroe']
>>>
Sign up to request clarification or add additional context in comments.

1 Comment

That was easier than i thought! Works perfectly. Really big thanks!

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.