def _all_pairs(i, contextsize, arrlen):
    """
    i: index in the array
    contextsize: size of the context around i
    arrlen: length of the array

    Returns iterator for index-tuples near i in a list of size s with
    context size @contextsize.  Context of k around i-th index means all
    substrings/subarrays of a[i-N:i+N+1] containing a[i]. 
    """
    return [ 
        (l,r) 
        # start anywhere between i - contextsize to i
        for l in range(max(i-contextsize, 0), i+1)
        # end anywhere between i to i + contextsize
        for r in range(i+1, min(i + 1 + contextsize, arrlen))
    ]