Im trying to find the index of each 1 that is at the end of each group(sequence) of 1s. The output for the example below should be [17, 12, 9, 5, 1].
I can find the 1 at index 17 using the formula below but I also need to find the 1 at index 12. (i.e. it's the last instance of 1 in each sequence (or group) of 1s)
list = [1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0] index_list1 = [i for i, v in enumerate(list) if v == 1] index_list0 = [i for i, v in enumerate(list) if v == 0] val1 = None for i in index_list1[::-1]: if i < index_list0[-1]: val1 = i break else: val1 = index_list1[-1] if val1 is not None: index1 = val1