def groupbysorted(iterable, keyfunc=None):

    """This is a variation of itertools.groupby.

    The itertools.groupby iterator assumes that the input is not sorted
    but will fit in memory.  This iterator has the same API, but assumes
    the opposite.

    Example usage::

        >>> for (key, subiter) in groupbysorted(
        ...     ((1, 1), (1, 2), (2, 1), (2, 3), (2, 9)), 
        ...     keyfunc=lambda row: row[0]):
        ...     print "New key:", key
        ...     for x in subiter:
        ...         print "Row:", x
        ... 
        New key: 1
        Row: (1, 1)
        Row: (1, 2)
        New key: 2
        Row: (2, 1)
        Row: (2, 3)
        Row: (2, 9)

    This requires the peekable class.  See my comment here_.

    Note, you must completely iterate over each subiter or groupbysorted will
    get confused.

    .. _here:
       http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/304373

    """

    iterable = peekable(iterable)

    if not keyfunc:
        def keyfunc(x):
            return x

    def peekkey():
        return keyfunc(iterable.peek())

    def subiter():
        while True:
            if peekkey() != currkey:
                break
            yield iterable.next()

    while True:
        currkey = peekkey()
        yield (currkey, subiter())
This is a variation of itertools.groupby.

    The itertools.groupby iterator assumes that the input is not sorted
    but will fit in memory.  This iterator has the same API, but assumes
    the opposite.

    Example usage::

        >>> for (key, subiter) in groupbysorted(
        ...     ((1, 1), (1, 2), (2, 1), (2, 3), (2, 9)), 
        ...     keyfunc=lambda row: row[0]):
        ...     print "New key:", key
        ...     for x in subiter:
        ...         print "Row:", x
        ... 
        New key: 1
        Row: (1, 1)
        Row: (1, 2)
        New key: 2
        Row: (2, 1)
        Row: (2, 3)
        Row: (2, 9)

    This requires the peekable class.  See my comment here_.

    Note, you must completely iterate over each subiter or groupbysorted will
    get confused.

    .. _here:
       http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/304373

    
recipes/Python/499379_GroupBySorted/recipe-499379.py