Why was there a [1] next to rsplit()?

This might be a really dumb question but why is [1] beside rplit() in the first lecture?

def shorten_param(param_name):
    if "__" in param_name:
        return param_name.rsplit("__", 1)[1]
    return param_name


cv_results = cv_results.rename(shorten_param, axis=1)
cv_results

The pattern for the parameter will be something like estimator__param_name. We are interesting to get the substring param_name. .rsplit is a string method that will split the string given a string (here __), and return the different part in a list. So here:

"estimator__param_name".rsplit("__", 1)

will give

['estimator', 'param_name']

Thus [1] is used to index and only get param_name.

1 Like

That cleared my doubt, thank you.

May I know what is the 1 inside the parenthesis is max_split? Should it take 2 elements during the split?

estimator__param_name".rsplit("__", 1)

I believe the 1 signifies the maximum number of splits being performed on a string and since there is only one “param” in a string, it’s a viable option to put max splits as 1

2 Likes

Indeed, we stop after the first split. The Python documentation is helpful for this purpose.

2 Likes