0

I have a list that looks like this

list = [blabla\nblabla\nblabla, wowwow\nwowow\n, etc]

I want to separate the list values based on the delimiter "\n" and create new lists. Like this:

list1 = [blabla, wowwow, etc] list2 = [blabla, wowow, etc]

Does anyone know how?

Thanks in advance!

titusAdam
  • 779
  • 1
  • 16
  • 35
  • 2
    take a look at `split`. Creating many lists is not ideal though. Maybe you could just have a list of lists? `new_list = [x.split('\n') for x in list]` And do not use the name *list* for a variable. Python uses that – Ma0 Dec 09 '16 at 16:08
  • You need to look into [`str.split()`](https://docs.python.org/3/library/stdtypes.html#str.split). – Christian Dean Dec 09 '16 at 16:09

1 Answers1

1

You can accomplish this task with using izip_longest (when the number of elements after the split is different) and list comprehension, like:

from itertools import izip_longest
n = ['blabla\nblabla\nblabla', 'wowwow\nwowow\n']
res = [v for v in izip_longest(*[k.split('\n') for k in n])]

For more info: https://docs.python.org/2/library/itertools.html#itertools.izip_longest

Antonio Beamud
  • 2,281
  • 1
  • 15
  • 26