0

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']
Anurag
  • 308
  • 2
  • 13

5 Answers5

1

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.

3dSpatialUser
  • 2,034
  • 1
  • 9
  • 18
  • That was nice explanation. As I mentioned in my question. The output should be list. But, In your case output is "str" – Bhargav - Retarded Skills Jan 18 '22 at 14:14
  • I'll edit my answer for you, but if you understand the principle, maybe you can try it yourself, see it as a learning way for the next problem you want to tackle with python and lists. – 3dSpatialUser Jan 18 '22 at 14:17
1

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.

joachimbf
  • 106
  • 8
1

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

joostblack
  • 2,465
  • 5
  • 14
1

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']

raz
  • 59
  • 4
0

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

Raviteja
  • 367
  • 2
  • 11