human
Complete this python program:
def _gemm_from_factored_list(lst):
assistant
#Here is the Python program you requested.

    """Returns None, or a list to replace node.outputs.
    """
    lst2 = []
    for sM in lst:
        if isinstance(sM, tuple):
            (sm0, sm1) = sM
            sm0 = T.as_tensor_variable(sm0)
            if (theano.scalar.upcast(sm0.dtype, sm1.dtype) == sm1.dtype):
                lst2.append((T.cast(sm0, sm1.dtype), sM[1]))
    lst = lst2
    def item_to_var(t):
        try:
            (s, M) = t
        except Exception:
            return t
        if (s == 1):
            return M
        if (s == (-1)):
            return (- M)
        return (s * M)
    for i in xrange((len(lst) - 1)):
        (s_i, M_i) = lst[i]
        for j in xrange((i + 1), len(lst)):
            (s_j, M_j) = lst[j]
            if (M_i.type != M_j.type):
                continue
            (gemm_of_sM_list, old_dot22) = _beta_L_plus_alpha_M(s_i, M_i, s_j, M_j)
            if gemm_of_sM_list:
                assert (len(gemm_of_sM_list) == 1)
                add_inputs = [item_to_var(input) for (k, input) in enumerate(lst) if (k not in (i, j))]
                add_inputs.extend(gemm_of_sM_list)
                if (len(add_inputs) > 1):
                    rval = [T.add(*add_inputs)]
                else:
                    rval = add_inputs
                return (rval, old_dot22)