def subInSexp(sexp, x, y):
    """Substitute (replace) `x` with `y` in `sexp`, i.e. returns `sexp[y/x]`
    """
    if type(sexp) != list:
        return (y if sexp == x else sexp)
    else:
        return [subInSexp(s, x, y) for s in sexp]