1

I have a single list that could be any amount of elements.

['jeff','ham','boat','','my','name','hello']

How do I split this one list into two lists or any amount of lists depending on blank string elements?

All these lists can then be put into one list of lists.

khelwood
  • 55,782
  • 14
  • 81
  • 108
Alice D
  • 33
  • 5
  • We can now write down any kind of solution - but did you try to solve this yourself first? Maybe you show us the code that you tried and we give you feedback on how to improve? – ingofreyer Oct 23 '18 at 07:40
  • That's where I'm stuck. I can make a variable that gives the number of lists I want but I have no idea what to do from there – Alice D Oct 23 '18 at 07:42
  • I saw already a few answers popping up. If you want to try this yourself without taking a predefined method, you could try to initialize an empty result list and a temporary list. iterate through your current list and check the element. if it is not empty, append it to the temporary list, otherwise append the temporary list to the result list and initialize a new temporary list. you just need to also append the temporary list once you reach the end of the iteration. This would not be the simples solution, but implementing it yourself gives you a good feeling on how to work with python lists. – ingofreyer Oct 23 '18 at 07:46
  • 1
    Possible duplicate of [Make Python Sublists from a list using a Separator](https://stackoverflow.com/questions/6164313/make-python-sublists-from-a-list-using-a-separator) – Austin Oct 23 '18 at 07:52

5 Answers5

4

If you are certain that there is only one blank string in the list, you can use str.index to find the index of the blank string, and then slice the list accordingly:

index = lst.index('')
[lst[:index], lst[index + 1:]]

If there could be more than one blank string in the list, you can use itertools.groupby like this:

lst = ['jeff','ham','boat','','my','name','hello','','hello','world']
from itertools import groupby
print([list(g) for k, g in groupby(lst, key=bool) if k])

This outputs:

[['jeff', 'ham', 'boat'], ['my', 'name', 'hello'], ['hello', 'world']]
blhsing
  • 91,368
  • 6
  • 71
  • 106
1

Using itertools.groupby, you can do:

from itertools import groupby

lst = ['jeff','ham','boat','','my','name','hello']

[list(g) for k, g in groupby(lst, key=bool) if k]
# [['jeff', 'ham', 'boat'], ['my', 'name', 'hello']]

Using bool as grouping key function makes use of the fact that the empty string is the only non-truthy string.

user2390182
  • 72,016
  • 6
  • 67
  • 89
0

This is one approach using a simple iteration.

Ex:

myList = ['jeff','ham','boat','','my','name','hello']
result = [[]]
for i in myList:
    if not i:
        result.append([])
    else:
        result[-1].append(i)
print(result)

Output:

[['jeff', 'ham', 'boat'], ['my', 'name', 'hello']]
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

Let list_string be your list. This should do the trick :

list_of_list=[[]]
for i in list_string:
    if len(i)>0:
        list_of_list[-1].append(i)
    else:
        list_of_list.append([])

Basically, you create a list of list, and you go through your original list of string, each time you encounter a word, you put it in the last list of your list of list, and each time you encounter '' , you create a new list in your list of list. The output for your example would be :

[['jeff','ham','boat'],['my','name','hello']]
Statistic Dean
  • 4,861
  • 7
  • 22
  • 46
0

i'm not sure that this is what you're trying to do, but try :

my_list = ['jeff','ham','boat','','my','name','','hello']
list_tmp = list(my_list)
final_list = []
while '' in list_tmp:
    idx = list_tmp.index('')
    final_list.append(list_tmp[:idx])
    list_tmp = list_tmp[idx + 1:]