Home > Software engineering >  How to split a list based on new line in the list
How to split a list based on new line in the list

Time:01-19

I've list like this.

list =    ['As Michael Harvey writes, paragraphs are “in essence—a form of punctuation, and like other former. ',
     '',
     'Many novice writers tend to make a sharp distinction between content and style, thinking that a paper can be strong']

As we can observer there is an empty list ''. I want to split my list based on the empty list.

Expected output:

#input

input list=['some text ', '', 'some texts example']

Output:

list1= ['some text ']
list2= ['some text example']

CodePudding user response:

Go over each element in the list en remove it when there is an element with length 0.

l =    ['As Michael Harvey writes, paragraphs are “in essence—a form of punctuation, and like other former. ',
     '',
     'Many novice writers tend to make a sharp distinction between content and style, thinking that a paper can be strong']

output = [ele for ele in l if len(ele) > 0]
list1, list2 = output

This works for your example, but it does not "split" on the empty element. Then you can use a for loop:

output = ['']

for ele in l:
    if len(ele) > 0:
        output[-1]  = ele
    else:
        output.append('')

This last part splits your list:

l = ['a', 'b', '', 'c']
output = ['']

for ele in l:
    if len(ele) > 0:
        output[-1]  = ele
    else:
        output.append('')
# output = ['ab', 'c']

Edit: output as lists:

l = ['a', 'b', '', 'c']
output = [[]]

for ele in l:
    if len(ele) > 0:
        output[-1].append(ele)
    else:
        output.append([])
# output = [['a', 'b'], ['c']]

If you have more lists it stops here since output can be as large as you want. If you know the result is two lists you can unpack them:

lst1, lst2 = output

sidenote: do not use variable list since it is a data structure in python.

CodePudding user response:

Super basic solution. Allows for splitting of more than two sentences. Output in sentences variable, a list of lists.

list = ['a','','b','','c']
sentences = []
for s in list:
    if len(s)!=0:
        sentences.append([s])

There are better and faster solutions in other answers here, this one just details a not-so-technical/beginner friendly solution.

CodePudding user response:

If you know how many lists you are going to have:

list1, *list2 = (i for i in input_list if i)

otherwise, in case you do not know how many lists you are going to end up with, you will probably end up with a dict or a list of list's

CodePudding user response:

You can use a dictionary of lists.

l =    ['As Michael Harvey writes, paragraphs are “in essence—a form of punctuation, and like other former. ',
     '',
     'Many novice writers tend to make a sharp distinction between content and style, thinking that a paper can be strong']

lists = dict()
x = 1
for ele in l:
  if(len(ele) > 0): # checking if the length of the element in l is sufficient (larger than 0)
    lists[x] = [ele] # adding a list with the element to the dictionary
    x  = 1

Output:

lists[1] = ['As Michael Harvey writes, paragraphs are “in essence—a form of punctuation, and like other former. ']

lists[2] = ['Many novice writers tend to make a sharp distinction between content and style, thinking that a paper can be strong']

CodePudding user response:

You can use the below-attached snippet for that.

list =    ['As Michael Harvey writes, paragraphs are “in essence—a form of punctuation, and like other former. ',
     '',
     'Many novice writers tend to make a sharp distinction between content and style, thinking that a paper can be strong']

out = [[i] for i in list if i!='']

# Printing each list
for i in out:
  print(i)

Output of the above code

Run the code here

  •  Tags:  
  • Related