def allcombinations(A, B, ordered):

    if ordered == "commutative":
        ordered = 11
    if ordered == "associative":
        ordered = None
    sm, bg = (A, B) if len(A) < len(B) else (B, A)
    for part in kbins(list(range(len(bg))), len(sm), ordered=ordered):
        if bg == B:
            yield tuple((a,) for a in A), partition(B, part)
        else:
            yield partition(A, part), tuple((b,) for b in B)
Restructure A and B to have the same number of elements
dataset/ETHPy150Open sympy/sympy/sympy/unify/core.py/allcombinations