def _group_runs(l, tolerance=2):
    """Generator to group contiguous chain ids and numbers in a list (l) with 
    a tolerance for  skipped numbers.

    Adapted from stackoverflow #21142231.

    Examples
    --------
    >>> g = _group_runs([('A', 2), ('A', 3), ('A', 4), 
    ...                  ('A', 8), ('A', 10), ('A', 12 )])
    >>> list(g)
    [[('A', 2), ('A', 3), ('A', 4)], [('A', 8), ('A', 10), ('A', 12)]]
    >>> g = _group_runs([('A', 2), ('A', 3), ('A', 4), 
    ...                  ('B', 4), ('B', 5), ('B', 6 )])
    >>> list(g)
    [[('A', 2), ('A', 3), ('A', 4)], [('B', 4), ('B', 5), ('B', 6)]]
    >>> list(_group_runs([]))
    []
    """
    l = sorted(l)
    out = []
    if l:
        last_chain = l[0][0]
        last_item = l[0][1]
        for i, j in l:
            if j - last_item > tolerance or i != last_chain:
                yield out
                out = []
            out.append((i, j))
            last_item = j
            last_chain = i
        yield out
