views:

58

answers:

3

Hi

I have a list as below

['Jellicle', 'Cats', 'are', 'black', 'and', 'white,\nJellicle', 'Cats', 'are', 'rather', 'small;\nJellicle', 'Cats', 'are', 'merry', 'and', 'bright,\nAnd', 'pleasant', 'to', 'hear', 'when', 'they', 'caterwaul.\nJellicle', 'Cats', 'have', 'cheerful', 'faces,\nJellicle', 'Cats', 'have', 'bright', 'black', 'eyes;\nThey', 'like', 'to', 'practise', 'their', 'airs', 'and', 'graces\nAnd', 'wait', 'for', 'the', 'Jellicle', 'Moon', 'to', 'rise.\n']

How can I remove the /n so I end up with a list with each word as a seperate thing with no /n.

Grammer is allowed to be left in the list.

thanks

+2  A: 

The simplest (although not the best performing) is probably to join then split:

l = ('\n'.join(l)).split('\n')

In fact it looks like you created this list by splitting on space. If so, you might want to reconsider how to create this list in the first place to avoid this extra step. You can split directly to the correct result by splitting on a regular expression matching whitespace, or better, by using s.split() without any arguments.

Mark Byers
In fact, if you call " foo bar\nbaz".split(), you'll get the right thing off the bat.
Ian
@Ian: +1 Good point. I'll modify my answer. :)
Mark Byers
+1  A: 
>>> [i for el in lst for i in el.splitlines()]
['Jellicle', 'Cats', 'are', 'black', 'and', 'white,', 'Jellicle', 'Cats', 'are', 'rather', 'small;', 'Jellicle', 'Cats', 'are', 'merry', 'and', 'bright,', 'And', 'pleasant', 'to', 'hear', 'when', 'they', 'caterwaul.', 'Jellicle', 'Cats', 'have', 'cheerful', 'faces,', 'Jellicle', 'Cats', 'have', 'bright', 'black', 'eyes;', 'They', 'like', 'to', 'practise', 'their', 'airs', 'and', 'graces', 'And', 'wait', 'for', 'the', 'Jellicle', 'Moon', 'to', 'rise.']
SilentGhost
A: 
>>> l = ['Jellicle', 'Cats', 'are', 'black', 'and', 'white,\nJellicle', 'Cats', 'are', 'rather', 'small;\nJellicle', 'Cats', 'are', 'merry', 'and', 'bright,\nAnd', 'pleasant', 'to', 'hear', 'when', 'they', 'caterwaul.\nJellicle', 'Cats', 'have', 'cheerful', 'faces,\nJellicle', 'Cats', 'have', 'bright', 'black', 'eyes;\nThey', 'like', 'to', 'practise', 'their', 'airs', 'and', 'graces\nAnd', 'wait', 'for', 'the', 'Jellicle', 'Moon', 'to', 'rise.\n']
>>> [i.strip(',;') for v in l for i in v.split()]
the_void