Q:

How to update previous iterator appened value conditionally?

I have a list of lists:
   my_list_of_lists =  [['a', 'keep me alone'],
                        ['b', 'keep me alone'],
                        ['c', 'Put me with previous value'],
                         ['d', 'keep me alone']]

I want to loop through my_list_of_lists and combine a value when the previous value when my_list[1] == 'Put me with previous value'.
The desired output is this:
my_updated_list_of_lists = [['a', 'keep me alone'], ['bc', 'keep me alone'], ['d', 'keep me alone']]

I have tried the following code but keep getting an IndexError: list index out of range error:
n = 0
my_updated_list_of_lists = []
for my_list in my_list_of_lists:
    n = n+1
    if my_list[1] == 'Put me with previous value':
        my_updated_list_of_lists[n-1][0] = my_updated_list_of_lists[n-1][0] + my_list[0]
        continue 
    else:
        my_updated_list_of_lists.append(my_list)

Because of the nature of the Put me with a previous value list item (it's a suffix and this is a part of speech feature I'm leveraging) I don't see it becoming the first entry in a list.
I would have expected the code to break if/when there was a problem, but I can't even get it to run.
Thank you for helping me!

A:

This should do it.
my_list_of_lists = [['a', 'keep me alone'],
                    ['b', 'keep me alone'],
                    ['c', 'Put me with previous value'],
                    ['d', 'keep me alone']]

my_updated_list_of_lists = []

for i in range(0, len(my_list_of_lists)):

    elem = my_list_of_lists[i]

    #Merge the first element of 2 consecutive items if second item contains Put me with previous value
    if i+1 < len(my_list_of_lists) and my_list_of_lists[i+1][1] == 'Put me with previous value':
        elem[0] = elem[0]+my_list_of_lists[i+1][0]
    #Ignore element containing Put me with previous value
    elif elem[1] == 'Put me with previous value':
        continue
    #Append element to new list
    my_updated_list_of_lists.append(elem)

print(my_updated_list_of_lists)
#[['a', 'keep me alone'], ['bc', 'keep me alone'], ['d', 'keep me alone']]

