def regroup(rdd, groups=10, check_first=False):
    """ Regroup an rdd using a new key added that is 0 ... number of groups - 1

    :param rdd: input rdd as a (k,v) pairs
    :param groups: number of groups to concatenate to
    :param check_first: check if first value is a key value pair.
    :return: a new rdd in the form of (groupNum, list of (k, v) in that group) pairs

    Example:

    >>> data = sc.parallelize(zip(range(4), range(4)))
    >>> data.collect()
    [(0, 0), (1, 1), (2, 2), (3, 3)]

    >>> data2 = regroup(data, 2)
    >>> data2.collect()
    [(0, [(0, 0), (2, 2)]), (1, [(1, 1), (3, 3)])]
    """
    if check_first:
        first = rdd.first()
        if isinstance(first, (list, tuple, collections.Iterable)):
            if len(first) != 2:
                raise ValueError('first item was not not length 2: %d' % len(first))
        else:
            raise ValueError('first item was wrong type: %s' % type(first))
    rdd = rdd.map(lambda kv: (kv[0] % groups, (kv[0], kv[1])), preservesPartitioning=True)
    return rdd.groupByKey().mapValues(list)