def recursive_sub(expr, replace):
    for _ in range(0, len(replace) + 1):
        new_expr = expr.subs(replace)
if new_expr == expr:
    return new_expr, True
else:
    expr = new_expr
return new_expr, False
Рекурсивная замена в sympy