def group_by(sequence, key):
    """
    Groups items of a sequence according to specified key selector and creates
    result values as (key, group) pairs.
    
    Args:
        sequence: iterable
            Sequence of items to go through.
        
        key: callable
            Item's key selector.
    
    Returns:
        ((any, (any,)),)
            Grouped items as (key, group) pairs.
    """
    
    keys = []
    groups = {}
    
    for item in sequence:
        k = key(item)
        
        if k not in groups:
            keys.append(k)
            groups[k] = []
        
        groups[k].append(item)
    
    return [(k, tuple(groups[k])) for k in keys]