How to iterate over a list of lists and update one of the sublists with content from previous iterations under certain conditions?

Question:

prev_names_elements = []  # keep track of names from previous iterations

for idx, i in enumerate(["1", "2", "3", "4"]):
    if   (i == "1"):  i_list = [['Luciana', 'María', 'Lucy'], ['jugando'], [], ['2023_-_03_-_07(19:00 pm_---_23:59 pm)']]
    elif (i == "2"):  i_list = [['ella NO DATA'], ['terminar el trabajo'], ['hacia el parque'], []]
    elif (i == "3"):  i_list = [['María', 'Melina Saez'], ['ordenaron', 'viajaron'], ['la casa'], ['2023_-_04_-_17(19:00 pm_---_23:59 pm)']]
    elif (i == "4"):  i_list = [['ellas NO DATA'], ['salieron de la casa juntos'], ['hacia el parque'], []]

    if (idx > 0):
        if 'ella NO DATA'  in i_list[0]:

        if 'ellas NO DATA' in i_list[0]:

    # update prev_names list with names from current i_list
    prev_names += [n for subl in i_list for n in subl if isinstance(subl, list)]

    print(i_list)
    print("n")

['ella NO DATA'] would take the last name of the first sublist of the list from the previous iteration of the for loop, in this case it would become ['Lucy'] , since it is the last of the elements of the first sublist of the list of lists from the previous iteration.

['ellas NO DATA'] would take all the names of all the first sublists of all the lists of the previous iterations, without repeating the names, so in this case it would look like ['Luciana', 'María', 'Lucy', 'Lucy', 'María', 'Melina Saez'] and since repetitions should be avoided, it would look like this [['Luciana', 'Lucy', 'María', 'Melina Saez']

In this way, when printing the i_list in each of the iterations of the for loop, you would be obtaining these 4 lists as output in the console:

[['Luciana', 'María', 'Lucy'], ['jugando'], [], ['2023_-_03_-_07(19:00 pm_---_23:59 pm)']]

[['Lucy'], ['terminar el trabajo'], ['hacia el parque'], []]

[['María', 'Melina Saez'], ['ordenaron', 'viajaron'], ['la casa'], ['2023_-_04_-_17(19:00 pm_---_23:59 pm)']]

[['Luciana', 'Lucy', 'María', 'Melina Saez'], ['salieron de la casa juntos'], ['hacia el parque'], []]

How should I set up this verification and replacement of the elements of the lists following the structure of this recursive test loop, to obtain this result?

Asked By: Matt095

||

Answers:

Just use the full list of names and use -1 indexing, and set() to get the unique elements:

prev_names_elements = []  # keep track of names from previous iterations

for idx, i in enumerate(["1", "2", "3", "4"]):
    if   (i == "1"):  i_list = [['Luciana', 'María', 'Lucy'], ['jugando'], [], ['2023_-_03_-_07(19:00 pm_---_23:59 pm)']]
    elif (i == "2"):  i_list = [['ella NO DATA'], ['terminar el trabajo'], ['hacia el parque'], []]
    elif (i == "3"):  i_list = [['María', 'Melina Saez'], ['ordenaron', 'viajaron'], ['la casa'], ['2023_-_04_-_17(19:00 pm_---_23:59 pm)']]
    elif (i == "4"):  i_list = [['ellas NO DATA'], ['salieron de la casa juntos'], ['hacia el parque'], []]

    if (idx > 0):
        if 'ella NO DATA'  in i_list[0]:
            i_list[0] = [prev_names_elements[-1]]
        if 'ellas NO DATA' in i_list[0]:
            i_list[0] = list(set(prev_names_elements))
    
    prev_names_elements.extend(i_list[0])

    print(i_list)
    print("n")

Output:

[['Luciana', 'María', 'Lucy'], ['jugando'], [], ['2023_-_03_-_07(19:00 pm_---_23:59 pm)']]


[['Lucy'], ['terminar el trabajo'], ['hacia el parque'], []]


[['María', 'Melina Saez'], ['ordenaron', 'viajaron'], ['la casa'], ['2023_-_04_-_17(19:00 pm_---_23:59 pm)']]


[['María', 'Lucy', 'Luciana', 'Melina Saez'], ['salieron de la casa juntos'], ['hacia el parque'], []]

Purposefully didn’t alter the rest of the code as I assume it is a toy version of what you are actually doing.

Answered By: B Remmelzwaal