def groupby(key_func):
    """
    Returns a function which returns a dictionary by grouping the given
    iterable by key_func.
    """
    def grouper(iterable):
        result = {}
        for item in iterable:
            values = result.setdefault(key_func(item), [])
            values.append(item)
        return result
    return grouper