1

I want to split the following list into sub lists based on an element from that list.

    array=['first','sentence','step','second','sentence']
    for i in array:
        if i!='step':
            newlist1.append(i)
        else:
            (stop appending in newlist1 and start appending in say newlist2)

newlist1 and newlist2 can not be pre declared. As the number of elements in the array can vary. SO I need to find a dynamic way of declaring lists as per the requirement.

3
  • should step be included in the second list? Commented May 21, 2021 at 10:56
  • no step is working as a separator element. It should preferably be removed Commented May 21, 2021 at 11:00
  • stackoverflow.com/questions/52590886/… Commented May 21, 2021 at 11:02

4 Answers 4

1

you could use a list of lists to store these. So if the value is step then start a new list, if not then append to the last list.

from pprint import pprint

lists = [[]]
array = ['first', 'sentence', 'step', 'second', 'sentence', 'step', 'thrid', 'step', 'some', 'other', 'sentance']
for i in array:
    if i == 'step':
        lists.append([])
    else:
        lists[-1].append(i)
pprint(lists)

OUTPUT

[['first', 'sentence'],
 ['second', 'sentence'],
 ['thrid'],
 ['some', 'other', 'sentance']]
Sign up to request clarification or add additional context in comments.

Comments

0

You can do the following:

array=['first','sentence','step','second','sentence']
newlist1 = []
newlist2 = []
check = True
for i in array:
    if i!='step' and check:
        newlist1.append(i)
    else:
        check = False
        newlist2.append(i)

1 Comment

This doesn't adhere to the OPs requirements that: the new lists be created dynamically as the original is of arbitrary length. It also ignores the clarification comment that 'step' be removed from the result.
0

Try this

index = array.index('step') 
if index and index < len(array):
    newlist1 = array[0:index+1]
    newlist2 = array[index+1:]

1 Comment

This doesn't adhere to the OPs requirements that: the new lists be created dynamically as the original is of arbitrary length. It also ignores the clarification comment that 'step' be removed from the result.
0

Although less memory efficient than Chris Doyle's answer, I prefer comprehensions for stuff like this unless they get too verbose to fit on one line.

array = ['first', 'sentence', 'step', 'second', 'sentence', 'step', 'third', 'step', 'some', 'other', 'sentence']

def split_list(lst, sep):
    return [i.split() for i in ' '.join(lst).split(sep)]

print(split_list(array, 'step'))

Result

[['first', 'sentence'], ['second', 'sentence'], ['third'], ['some', 'other', 'sentence']]

Bonus

If the end goal is a list of sentences instead of a list of lists, just replace the first .split() with .strip().

[i.strip() for i in ' '.join(lst).split(sep)]

Returns:

['first sentence', 'second sentence', 'third', 'some other sentence']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.