Nested list mapping

main_list = [[‘livingroom’, ‘Entrance’, ‘livingroom’, ‘bedroom’, ‘kitchen’, ‘bathroom’],
[‘bedroom’, ‘Entrance’, ‘livingroom’, ‘bathroom’, ‘livingroom’, ‘kitchen’],
[‘bedroom’, ‘Entrance’, ‘bathroom’, ‘bathroom’, ‘livingroom’, ‘livingroom’, ‘bathroom’, ‘livingroom’, ‘bedroom’, ‘kitchen’],
[‘bedroom’, ‘Entrance’, ‘bathroom’, ‘bathroom’, ‘bathroom’, ‘kitchen’, ‘livingroom’, ‘livingroom’, ‘bedroom’, ‘bathroom’]]

neighbors = [[‘Entrance’, ‘bedroom’, ‘livingroom’], [‘livingroom’, ‘livingroom’, ‘kitchen’, ‘Entrance’], [‘Entrance’, ‘bathroom’, ‘livingroom’], [‘livingroom’, ‘kitchen’, ‘bedroom’], [‘Entrance’, ‘bedroom’, ‘bathroom’, ‘kitchen’], [‘livingroom’, ‘kitchen’, ‘bathroom’],
[‘Entrance’, ‘livingroom’, ‘bedroom’], [‘bedroom’, ‘bathroom’, ‘Entrance’], [‘bedroom’, ‘bathroom’, ‘livingroom’], [‘Entrance’, ‘livingroom’, ‘livingroom’, ‘bathroom’], [‘bathroom’, ‘kitchen’, ‘livingroom’], [‘livingroom’, ‘kitchen’],
[‘Entrance’, ‘bathroom’, ‘livingroom’, ‘bedroom’], [‘bedroom’, ‘bathroom’, ‘livingroom’, ‘Entrance’], [‘bedroom’, ‘bathroom’, ‘bathroom’], [‘Entrance’, ‘livingroom’, ‘bedroom’, ‘bathroom’], [‘Entrance’, ‘livingroom’, ‘livingroom’, ‘livingroom’], [‘bedroom’, ‘livingroom’, ‘bathroom’, ‘livingroom’], [‘bathroom’, ‘livingroom’, ‘bathroom’], [‘bathroom’, ‘livingroom’, ‘kitchen’, ‘livingroom’], [‘bathroom’, ‘kitchen’, ‘bedroom’], [‘livingroom’, ‘bedroom’, ‘kitchen’],
[‘Entrance’, ‘bathroom’, ‘bathroom’, ‘bedroom’], [‘bedroom’, ‘bathroom’, ‘Entrance’], [‘bedroom’, ‘kitchen’, ‘livingroom’, ‘bathroom’], [‘Entrance’, ‘bathroom’, ‘livingroom’, ‘bathroom’], [‘bedroom’, ‘bathroom’, ‘kitchen’, ‘bathroom’], [‘bathroom’, ‘bathroom’, ‘bedroom’, ‘kitchen’], [‘bathroom’, ‘bathroom’, ‘livingroom’], [‘bathroom’, ‘bedroom’, ‘livingroom’], [‘kitchen’, ‘livingroom’, ‘bedroom’], [‘livingroom’, ‘bathroom’]]

each item in main list in each sublist in neighbors check if the last item in each sublist in neighbors is ‘livingroom’ and other item in sublist is ‘bedroom’ find the place of this last item in main list and remove it

I want this output: [[‘Entrance’, ‘livingroom’, ‘bedroom’, ‘kitchen’, ‘bathroom’],
[‘bedroom’, ‘Entrance’, ‘bathroom’, ‘livingroom’, ‘kitchen’],
[‘bedroom’, ‘Entrance’, ‘bathroom’, ‘bathroom’, ‘livingroom’, ‘bathroom’, ‘livingroom’, ‘bedroom’, ‘kitchen’],
[‘bedroom’, ‘Entrance’, ‘bathroom’, ‘bathroom’, ‘bathroom’, ‘kitchen’, ‘livingroom’, ‘bedroom’, ‘bathroom’]]

Sorry, I don’t speak Roomanian. Google Translate either… But, if you mark your question with a question mark, and write the question in simple English, we might help. If you fail to do that, then you might throw everything away and start again.

1 Like

Hi this should do it, I think

def get_nested_index(main_list, i):
    for main_index, sublist in enumerate(main_list):
        if i < len(sublist):
            sub_index = i
            break
        i -= len(sublist)
    return main_index, sub_index


to_be_removed = []
i = 0
for sublist in neighbors:
    if sublist[-1] == "livingroom" and "bedroom" in sublist:
        to_be_removed.append(i)
    i +=1

while to_be_removed:
    # work in reverse order to avoid messing up later index numbers
    i = to_be_removed.pop()
    i, j = get_nested_index(main_list, i)
    del main_list[i][j]