def xcombinations(s, K):
    """
    On entry: s sequence of items; K size of the combinations
    Returns: generator of the K-combinations without replacement of
    s.
    """
    N = len(s)
    assert K<=N, 'Error K must be less or igual than N'
    S = [[] for i in range(K) ]
    for n in range(1,N+1):
        newS = [[] for i in range(K) ]
        for k in range(max(1, n-(N-K)), min(K+1, n+1)):
            if k == K:
                for el in S[k-1]:
                    yield el+[s[n-1]] 
            else:
                newS[k].extend(S[k])
                if len(S[k-1])==0:
                    newS[k].append([s[n-1]])
                else:
                    newS[k].extend( [el+[s[n-1]] for el in S[k-1]] )
        S = newS

    On entry: s sequence of items; K size of the combinations
    Returns: generator of the K-combinations without replacement of
    s.
    
recipes/Python/577766_Generator_combinations_without_replacement/recipe-577766.py