def stitch_matched_regions(cigartuples):
    """
    Close gaps that are created by I or D events in cigar tuples.

    :param cigartuples:
    :return:
    >>> stitch_matched_regions([(4, 90), (0, 15), (1, 1), (0, 19)])
    [(4, 90), (0, 35)]
    >>> stitch_matched_regions([(4, 90), (0, 15), (1, 1), (0, 19), (5, 30)])
    [(4, 90), (0, 35), (5, 30)]
    >>> stitch_matched_regions([(4, 90), (0, 15), (2, 1), (0, 21)])
    [(4, 90), (0, 35)]
    >>> stitch_matched_regions([(4, 90), (0, 15), (1, 10), (0, 19)])
    [(4, 90), (0, 15), (1, 10), (0, 19)]
    >>> stitch_matched_regions([(4, 90), (0, 15), (2, 1), (0, 21), (7, 5)])  # Test that we don't produce an index error.
    [(4, 90), (0, 35)]
    >>> stitch_matched_regions([(1, 90), (0, 15), (1, 1), (0, 19)])
    [(0, 35)]
    """
    new_tuples = []
    for i, (op, l) in enumerate(cigartuples):
        if op in {1, 2, 7, 8}:
            if new_tuples and new_tuples[-1][0] == 0 and len(cigartuples
                ) != i + 1 and cigartuples[i + 1][0] == 0:
                previous_tuple = new_tuples.pop()
                add_length = previous_tuple[1]
                next_tuple = cigartuples.pop(i + 1)
                if op == 1:
                    add_length += l
                if op == 2:
                    add_length -= l
                if l < (next_tuple[1] + add_length) / 10.0:
                    cigartuples.insert(i + 1, (next_tuple[0], next_tuple[1] +
                        add_length))
                else:
                    new_tuples.append(previous_tuple)
                    new_tuples.append((op, l))
                    cigartuples.insert(i + 1, next_tuple)
        else:
            new_tuples.append((op, l))
    return new_tuples
