def groupby2(*args):
generatorList = [] <EOL>if len(args) % <NUM_LIT:2> == <NUM_LIT:1>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>advanceList = []<EOL>for i in xrange(<NUM_LIT:0>, len(args), <NUM_LIT:2>):<EOL><INDENT>listn = args[i]<EOL>fn = args[i + <NUM_LIT:1>]<EOL>if listn is not None:<EOL><INDENT>generatorList.append(groupby(listn, fn))<EOL>advanceList.append(True) <EOL><DEDENT>else:<EOL><INDENT>generatorList.append(None)<EOL>advanceList.append(False)<EOL><DEDENT><DEDENT>n = len(generatorList)<EOL>nextList = [None] * n<EOL>while True:<EOL><INDENT>for i in xrange(n):<EOL><INDENT>if advanceList[i]:<EOL><INDENT>try:<EOL><INDENT>nextList[i] = generatorList[i].next()<EOL><DEDENT>except StopIteration:<EOL><INDENT>nextList[i] = None<EOL><DEDENT><DEDENT><DEDENT>if all(entry is None for entry in nextList):<EOL><INDENT>break<EOL><DEDENT>minKeyVal = min(nextVal[<NUM_LIT:0>] for nextVal in nextList<EOL>if nextVal is not None)<EOL>retGroups = [minKeyVal]<EOL>for i in xrange(n):<EOL><INDENT>if nextList[i] is not None and nextList[i][<NUM_LIT:0>] == minKeyVal:<EOL><INDENT>retGroups.append(nextList[i][<NUM_LIT:1>])<EOL>advanceList[i] = True<EOL><DEDENT>else:<EOL><INDENT>advanceList[i] = False<EOL>retGroups.append(None)<EOL><DEDENT><DEDENT>yield tuple(retGroups)<EOL><DEDENT>
Like itertools.groupby, with the following additions:

    - Supports multiple sequences. Instead of returning (k, g), each iteration
      returns (k, g0, g1, ...), with one `g` for each input sequence. The value of
      each `g` is either a non-empty iterator or `None`.
    - It treats the value `None` as an empty sequence. So you can make subsequent
      calls to groupby2 on any `g` value.

    .. note:: Read up on groupby here:
           https://docs.python.org/dev/library/itertools.html#itertools.groupby

    :param args: (list) Parameters alternating between sorted lists and their
                        respective key functions. The lists should be sorted with
                        respect to their key function.

    :returns: (tuple) A n + 1 dimensional tuple, where the first element is the
                    key of the iteration, and the other n entries are groups of
                    objects that share this key. Each group corresponds to the an
                    input sequence. `groupby2` is a generator that returns a tuple
                    for every iteration. If an input sequence has no members with
                    the current key, None is returned in place of a generator.