[Solved] Concat elements of tuple if contiguous lables are same


Here’s a quick function to do this looping through the tuples and comparing each tuple to the label of the previous tuple and concatenating them if the labels match.

def parse_tuples(x):
    prev_tuple = list(x[0])
    parsed = []
    for i in x[1:]:
        if i[1] == prev_tuple[1]:
            prev_tuple[0] += i[0]
        else:
            parsed.append(tuple(prev_tuple))
            prev_tuple = list(i)
    parsed.append(tuple(prev_tuple))
    return parsed

solved Concat elements of tuple if contiguous lables are same