'python:split a list of strings into multiple lists before a certain value

I have a list as follows,

my_list = ['France, the weather is nice', 'the house is beautiful', 'we have a fresh start here','France, the dinner is amazing', 'the lady is lovely', 'France, the house is beautiful','the location is fascinating']

I want to split the list into multiple list every time the word 'France,' is in the string, so the output should be,

desired_list = [['France, the weather is nice', 'the house is beautiful', 'we have a fresh start here'],['France, the dinner is amazing', 'the lady is lovely'],['France, the house is beautiful','the location is fascinating']]

I have tried the following, but would like to keep the sentence that has 'France,' too.

new_list =[list(y) for x, y in itertools.groupby(my_list, lambda z: z.startswith('France,)) if not x]

another answer

in addition to the answer given by Titouan L, I also found the answer using more_itertools

import more_itertools as miter
print(list(miter.split_before(my_list, lambda x: x.startswith("France,"))))


Solution 1:[1]

Just for fun, using recursion:

my_list = ['France, the weather is nice', 'the house is beautiful', 'we have a fresh start here', 'France, the dinner is amazing', 'the lady is lovely', 'France, the house is beautiful','the location is fascinating']


def split_list(index=len(my_list) - 1):
    if index < 0:
        return []
    if not my_list[index].startswith('France,'):
        data = split_list(index - 1)
        data[-1].append(my_list[index])
        return data
    else:
        return split_list(index - 1) + [[my_list[index]]]


print(split_list())

Output:

[['France, the weather is nice', 'the house is beautiful', 'we have a fresh start here'], ['France, the dinner is amazing', 'the lady is lovely'], ['France, the house is beautiful', 'the location is fascinating', 'the location is fascinating']]

Definitely not the most efficient :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cubix48