Could somebody help me to split a list into a list of lists by element ('#')?
Input:
['1', '2','#','3','4','#','5']
Output:
[['1', '2'], ['3','4'], ['5']]
Could somebody help me to split a list into a list of lists by element ('#')?
Input:
['1', '2','#','3','4','#','5']
Output:
[['1', '2'], ['3','4'], ['5']]
input = ['1', '2','#','3','4','#','5']
s = ''.join(input).split('#')
r = []
for i in s:
r.append(list(i))
output = r
Use string join and split method:
alist= ['1', '2','#','3','4','#','5']
as_string = ' '.join(alist).split('#')
as_string_list = [i.strip().split(' ') for i in as_string]
print as_string_list
Here is your answer:
[list(x[1]) for x in itertools.groupby(myList, lambda x: x=='#') if not x[0]]
This question is a duplicate of Make Python Sublists from a list using a Separator. That is were the modified answer comes from.