[Python-ideas] string.replace should accept a list as a first argument

MRAB python at mrabarnett.plus.com
Tue Oct 6 19:03:38 CEST 2015


On 2015-10-06 20:25, Emil Rosendahl Petersen wrote:
> I think string.replace should be changed accept a list as a first
> argument.
>
> That way, if I had this string:
>
> "There are a lot of undesirable people in this filthy world"
>
> Then I could do this, replace(['undesirable', 'filthy'], ''), in case
> that's what I wanted to do.
>
> Now, string.replace doesn't accept a list as its first argument, and
> complains about implicit conversion.
>
> Is there any great obstacle to just having the function loop over that
> list, calling itself in case we get a list argument instead of a str?
>
> Doesn't that seem like the more obvious behaviour? To me the results of
> running the above code should be unsurprising, if this change was
> implemented: "there are a lot of people in this world".
>
Looping over the list is the wrong way to do it because each pass might
produce a string that leads to different matches for the subsequent
passes.

I think that the replacements should be done based on the earliest
longest match.



More information about the Python-ideas mailing list