Jmariz
Jmariz

Reputation: 245

Joining split list into a list

I'm trying to take a split up list and join them into another list. For example, I have this list:

['T', 'e', 's', 't', '\n', 'List', '\n']
Now I want to join these so it looks like
['Test', 'List']
How can I do this?

Upvotes: 0

Views: 5692

Answers (2)

Mark Longair
Mark Longair

Reputation: 468241

I'm afraid that your question is a little underspecified, as S. Lott comments, but it looks as if you just want to join all the strings together and then split where there are newlines - the following works for your example, and could be easily modified for other requirements:

>>>> ''.join(['T', 'e', 's', 't', '\n', 'List', '\n']).splitlines()
['Test', 'List']

Upvotes: 7

Doug T.
Doug T.

Reputation: 65649

string joining is an amazing thing

l = ['T', 'e', 's', 't', '\n', 'List', '\n']
"".join(l).split('\n')

Works by taking a "" string, creating a larger string by appending all of l to it giving "Test\nList\n". Then splitting on end of line giving ["Test", "List"]

Upvotes: 4

Related Questions