def merge(spans): """Merge overlapping [start, end] spans. Currently only merges neighbours that were already adjacent in the input.""" if not spans: return [] out = [list(spans[0])] for start, end in spans[1:]: last = out[-1] if start == last[1]: last[1] = end else: out.append([start, end]) return [tuple(s) for s in out]